Lcd screen prints weird symbols instead of numbers - arduino

I am trying to make a alarm clock with the raspberry pi and an arduino. I have been having this problem that when i use serial communication to send a number it, the lcd doesnt print the number. I know that the arduino is getting the number, for some reason it just wont print it. It instead prints weird symbols and lines. This article shows how i use serial communication between themThis is my arduino code.
#include <LiquidCrystal.h>
const int ledPin = 13;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,1);
lcd.print("crystralball");
}
void loop()
{
if (Serial.available())
{
flash(Serial.read() - '0');
}
delay(1000);
}
void flash(int n)
{
for (int i = 0; i < n; i++)
{
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.write(n);
Serial.print(n);
Serial.flush();
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}

Hi try to change your code in loop like this.
for (int i = 0; i < n; i++){
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.print(String(n));
Serial.print(n);
Serial.flush();
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
You have to use the method print and passing a string.

Related

Problem with blinking a led with bluetooth in arduino

i am trying to make a program that turns on ,off and blinks an led with the help from bluetooth
On and of were pretty easy to replicate,but i can't make the blink to work.There are to options either blinks once,either if i ad a while it never stops from looping.i tried with both if and case.Can somebody help me.I am using an esp32.
The code with if:
#include "BluetoothSerial.h"
#include <Arduino.h>
#include <analogWrite.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='a';
const char turnOFF ='b';
const char turnBLINK= 'c';
//const char turnFADE='d';
const int LEDpin = 12;
//int brightStep = 1;
//int brightness = 0;
void setup() {
Serial.begin(115200);
SerialBT.begin("Mono"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: a");//print on serial monitor
Serial.println("To turn OFF send: b"); //print on serial monitor
pinMode(LEDpin, OUTPUT);
// analogWriteResolution(LEDpin, 12);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
// while(SerialBT.available()){
// receivedChar =(char)SerialBT.read();
// }
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("LED ON:");// write on BT app
Serial.println("LED ON:");//write on serial monitor
digitalWrite(LEDpin, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("LED OFF:");// write on BT app
Serial.println("LED OFF:");//write on serial monitor
digitalWrite(LEDpin, LOW);// turn the LED off
}
if(receivedChar == turnBLINK)
{
SerialBT.println("LED blink:");// write on BT app
Serial.println("LED blink:");//write on serial monitor
while (receivedChar == turnBLINK){
//receivedChar =(char)SerialBT.read();
//if(receivedChar != turnBLINK){
// break;
// } else {
digitalWrite(LEDpin, HIGH);// turn the LED off
delay(1000);
digitalWrite(LEDpin,LOW);
delay(1000);
if(receivedChar != turnBLINK){
break; }
}
}
}
delay(20);
}
and with case:
#include "BluetoothSerial.h"
#include <Arduino.h>
#include <analogWrite.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
char data;
int option;
int blink=0;
const int LEDpin = 12;
void setup() {
Serial.begin(115200);
SerialBT.begin("Mono"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: 1");//print on serial monitor
Serial.println("To turn OFF send: 0"); //print on serial monitor
pinMode(12, OUTPUT);
}
void loop(){
receivedChar =(char)SerialBT.read();
// if (Serial.available()) {
// SerialBT.write(Serial.read());
// }
if (SerialBT.available()) {
// while(SerialBT.available()){
// receivedChar =(char)SerialBT.read();
// }
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
data=receivedChar;
if(data == '0')
{
option = 0;
blink=0;
}else if(data == '1')
{
option = 1;
blink=0;
}else if(data == '2')
{
option = 2;
blink=1;
}
switch (option)
{
case 0: // LED OFF
digitalWrite(LEDpin, LOW);
break;
case 1: //LED ON
digitalWrite(LEDpin, HIGH);
break;
case 2:
while(blink=1){// LED BLINK
digitalWrite(LEDpin , HIGH);
delay(200);
digitalWrite(LEDpin, LOW);
delay(200);
}
break;
}
}
}
First of all, you should clean your code to a minimum reproducible example. Remove all unnecessary comments and pieces of code that do not represent the main problem you are facing.
After a quick skim over your code, I immediately noticed this:
while(blink=1){// LED BLINK
digitalWrite(LEDpin , HIGH);
delay(200);
digitalWrite(LEDpin, LOW);
delay(200);
}
where it should be while(blink==1){ } --> very common mistake. This should be a comparison, NOT an assignment.
Now, you mention that it never stops running. Even after correcting the error I just pointed at, what part inside of your while loop breaks the logic of blink from being equal to 1? Otherwise, the while-loop will never stop
Finally, do not read the serial data inside the main loop(). Use SerialEvent, rather.
Again, it is quite tricky to follow the flow of your code. I suggest you divide your code into functions in order to make it more readable.

Why does VirtualWire seem to receive transmission twice?

I have a simple setup with two Arduinos and a 433MHz transmitter and a receiver module.
The transmitter is set to transmit a string on button press, that side works correctly.
On the receiver end I have a simple program that's just writing whatever it's receiving to serial:
#include "VirtualWire.h"
int rx_pin = 2;
void setup(){
Serial.begin(9600);
Serial.println("serial ready");
vw_set_rx_pin(rx_pin);
vw_setup(2000);
vw_rx_start();
Serial.println("receiver ready");
}
void loop(){
uint8_t msg[VW_MAX_MESSAGE_LEN];
uint8_t msglen = VW_MAX_MESSAGE_LEN;
vw_wait_rx();
if(vw_get_message(msg, &msglen)){
Serial.print("Got: ");
for (int i = 0; i < msglen; i++)
{
Serial.print(char(msg[i]));
}
Serial.println();
}
}
When I then monitor serial, the receiving Arduino seems to receive the message twice each time it's sent. I used an oscilloscope to verify (to the best of my knowledge) that the transmitter is only sending the message once, I also tried wiring the two Arduinos together to make sure the issue is not with the RF modules, I got the same results.
This makes me think there is an issue with my code or with VirtualWire itself.
I'm thinking that I should somehow check if I've received the same message already or I should clear VirtualWire's buffer, how would I do either of those?
EDIT:
Here is the transmitter code:
#include "VirtualWire.h"
int tx_pin =2;
int interruptPin = 3;
volatile bool transmitBool = false;
void setup(){
vw_set_tx_pin(tx_pin);
vw_setup(2000);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), transmit, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
transmitBool = false;
}
void loop(){
if(transmitBool) {
transmitBool = false;
digitalWrite(13, HIGH);
vw_send((uint8_t *)"abc", 4);
vw_wait_tx();
delay(500);
digitalWrite(13, LOW);
}
}
void transmit() {
transmitBool = true;
}

RFid click board with CR95HF microcontroller refuses to respond

I have an Arduino with a RFid click board from Mikro Electronika. The click board has a microcontroller CR95HF microcontroller. According to the datasheet when I send the ECHO command I should poll and then read the returned value 0x55. If I use the command IDN (0x01) and send a datalength of zero, I should get the ID of the controller back.
According to the serial monitor it only returns zero (is there something wrong with the variable?).
According to the logic analyzer it returns the exact same thing as what was send
(when I send 3, it returns 3, when i send 55, it returns 55).
It seems the microcontroller doesn't react anything I send it or it does and it's just broken?
So right now it looks like it is able to send something through the spi.transfer() function, but the retrieve seems to be a problem.
Here's my code:
#include <SPI.h>
// CR95HF: Write, poll, read, reset
#define CMD 0x00
#define POLL 0x03
#define READ 0x02
#define RESET 0x01
#define ECHO 0x55
const int _SSpin = 10; // CS
const int _IRQpin = 6; // INT_out : 2, INT_in : 6, tried both, barely to no difference
//const int _SI0pin = A3;
//const int _SI1pin = A0;
void WakeupCR95HF()
{
//Serial.println("Wake up!");
digitalWrite(_IRQpin, LOW);
delay(100);
digitalWrite(_IRQpin, HIGH);
delay(100);
}
bool EchoResponse()
{
byte len = 0;
// write to CR95HF
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x00);
// Serial.print("echo code: ");
// Serial.println(ECHO);
SPI.transfer(0x55);
//SPI.transfer(0x00);
digitalWrite(_SSpin, HIGH);
//delay(1000);
// poll to CR95HF, is it ready?
byte tmp = 0;
digitalWrite(_SSpin, LOW);
while (!tmp)
{
delay(100);
//Note: whatever I send, I get the same thing back. I send 3, then tmp is 3. I send 8, tmp is 8.
SPI.transfer(0x03);
tmp = SPI.transfer(0);
// Serial.print("Polling: ");
Serial.println(tmp, BIN);
tmp = (tmp & 0x08) >> 3;
delay(100);
}
digitalWrite(_SSpin, HIGH);
delay(20);
// ready to write
byte res = 0;
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x02);
res = SPI.transfer(0);
// Serial.print("reading: ");
// Serial.println(res);
delay(100);
digitalWrite(_SSpin, HIGH);
delay(100);
if (res == ECHO)
{
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
// I believe this is not possible on Arduino or not nessaccery, tried it anyway, no change
// pinMode(_SI0pin, OUTPUT);
// pinMode(_SI1pin, OUTPUT);
pinMode(_SSpin, OUTPUT);
pinMode(_IRQpin, OUTPUT);
// digitalWrite(_SI0pin, HIGH);
// digitalWrite(_SI1pin, LOW);
digitalWrite(_IRQpin, HIGH); // wakeup
delay(20);
digitalWrite(_SSpin, HIGH); // slave select, low = ready to write/read
delay(20);
SPI.begin();
// 2 MHz max clock speed, Most Significant Bit first, SPI_MODE unknown (probably 0, all were tried)
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
WakeupCR95HF();
while(!EchoResponse())
{
WakeupCR95HF();
}
// Serial.println("Connection established");
}
void loop() {
}

Serial.read() - XBee - not firing 'else if'

I am currently trying to do some communication test between a PC and Arduino Uno using an XBee in AT mode.
My test is to send characters from the computer to the XBee and process through conditional statements.
I don't believe this issue is with configuration of the XBees, for I am able to communicate successfully when I watch the Serial monitors.
Here is the code I am running on the Arduino:
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
if (xBee.available()> 0)
{
if (xBee.read() == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if (xBee.read() == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
I can turn on the LED when sending the character 'r' from the PC to the XBee. The intended result is received back as well. When I try to send the character 'o' from the PC the LED stays on, and I get the response of "NR".
This same result happens with different characters in the else if statement, sending character 'o' as the first character, changing to just if statements, and changing the initial condition to - while xBee.available().
How can I fix this problem?
You need to store the input value of xBee.read() and then use it in the if condition.
You can try this
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
char read_value = xBee.read();
if(xBee.available()> 0)
{
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if ( read_value == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
The problem is that you are taking the input with xBee.read() but not storing it.
Only your first if works ie,
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
The control is not even going in the else if hence condition for o is not tested.

Arduino Leonardo Endless Loop

My Code:
*int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void loop() {
}*
So this is the problem. the simple program waiting for Serial and the while loop continue forever.
how to fix this. is it a known problem?
int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial1) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
}
void loop() {
}
I think that is a missing bracket closing setup()
Your loop condition is wrong. If you want to wait for something to appear on the serial port then you need to change it to this:
while (Serial.available() == 0) {
Your code is fine, it looks like your serial port is not connecting. If I modify your code to watch a counter up to 5, and when the counter is 5 then myconnection is true, then !myconnection is false and the loop stops (watch it in Tools>Serial Monitor):
int led = 13;
int counter = 0;
boolean myconnection = false;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!myconnection) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
//here's my debug stuff
counter++;
if (counter ==5 ){
myconnection = true;
}
Serial.println("counter: " + String(counter) + ", myconnection: " + String(myconnection));
}
}
void loop() {
}

Resources