LCD Arduino not displaying correctly - arduino

My problem is when I input the string in the serial monitor it shows like this:
LCD Arduino Error
The setCursor dont work and also there is another weird character created before the actual output.
This is my sample code:
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
lcd.print("hello, world!");
}
void loop() {
String readString;
String Q;
while (Serial.available()) {
delay(1);
if (Serial.available()>0) {
char c = Serial.read();
if(isControl(c)){
break;
}
readString += c;
}
}
Q = readString;
if (Q == "1"){
lcd.setCursor(0,1);
lcd.print("Hello");
}
if (Q == "2"){
lcd.setCursor(0,1);
lcd.print("World");
}
}

First of all you should understand the LCD lib functions. To set the cursorto theFirst row you need
lcd.setCursor(0,0); // row index starts with 0
if you only set the cursor back without clearing the screen there might be weird chars,sodo a
lcd.clear(); //clears the whole screen
OR define an empty String:
String lineClear =" "; // should be 16 spaces for a 16x2 display
and do as a clearing sequence (e.g. for the top line)
lcd.setCursor(0,0);
lcd.print(lineClear);
lcd.print("Hello");
Remember the syntax is
lcd.setCursor(col, row)
// index for 16x2 is col 0-15,row 0-1
// index for 20x4 is col 0-19,row 0-3
and in setup alwas do a
lcd.clear();
after initializing the lcd, to remove possible artefacts from the buffer

Related

How to stop multiple reads of an RFID card

I'm using an 125Khz RFID module RDM6300 with arduino nano.
While the card is near the RFID reader the loop will read the card multiple times. I want it to read only once while the card is near the reader then read it again if a new connection is being made.
*This code is not writen by me, this is the source:
https://github.com/Wookai/arduino-rfid
// define constants for pins
//int SUCCESS = 10;
//int ERROR = 13;
// variables to keep state
int readVal = 0; // individual character read from serial
unsigned int readData[10]; // data read from serial
int counter = -1; // counter to keep position in the buffer
char tagId[11]; // final tag ID converted to a string
char* authorizedTags[4]; // array to hold the list of authorized tags
// fills the list of authorzied tags
void initAuthorizedTags() {
// add your own tag IDs here
authorizedTags[0] = "0400680B85";
authorizedTags[1] = "0400063EB9";
authorizedTags[2] = "040004F3F5";
authorizedTags[3] = "04006813AB";
}
void setup() {
Serial.begin(9600);
// pinMode(SUCCESS, OUTPUT);
//pinMode(ERROR, OUTPUT);
initAuthorizedTags();
}
// check if the tag ID we just read is any of the authorized tags
int checkTag() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags[i], tagId) == 0) {
return 1;
}
}
return 0;
}
// convert the int values read from serial to ASCII chars
void parseTag() {
int i;
for (i = 0; i < 10; ++i) {
tagId[i] = readData[i];
}
tagId[10] = 0;
}
// once a whole tag is read, process it
void processTag() {
// convert id to a string
parseTag();
// print it
printTag();
// check if the tag is authorized
if (checkTag() == 1) {
tagSuccess(); // if so, perform an action (blink a led, open a door, etc...)
} else {
tagFailed(); // otherwise, inform user of failure
}
}
void printTag() {
Serial.print("Tag value: ");
Serial.println(tagId);
}
// perform an action when an authorized tag was read
void tagSuccess() {
Serial.println("Tag authorized.");
// here, we simply turn on the success LED for 2s
// digitalWrite(SUCCESS, HIGH);
//digitalWrite(ERROR, LOW);
// delay(2000);
}
// inform the user that the tag is not authorized
void tagFailed() {
Serial.println("Unauthorized access!");
//digitalWrite(SUCCESS, LOW);
// digitalWrite(ERROR, HIGH);
// delay(2000);
}
// this function clears the rest of data on the serial, to prevent multiple scans
void clearSerial() {
while (Serial.read() >= 0) {
; // do nothing
}
}
void loop() {
// turn LEDs off
// digitalWrite(SUCCESS, LOW);
// digitalWrite(ERROR, LOW);
if (Serial.available() > 0) {
// read the incoming byte:
readVal = Serial.read();
// a "2" signals the beginning of a tag
if (readVal == 2) {
counter = 0; // start reading
}
// a "3" signals the end of a tag
else if (readVal == 3) {
// process the tag we just read
processTag();
// clear serial to prevent multiple reads
clearSerial();
// reset reading state
counter = -1;
}
// if we are in the middle of reading a tag
else if (counter >= 0) {
// save valuee
readData[counter] = readVal;
// increment counter
++counter;
}
}
}
Thank you.
Thank you for your answers. I tried to accept the multiple reads and print only one but it keeps printing "already read" instead of reading the card first time, this is the code:
void printTag() {
if(strcmp(tagId,previous)==1){
strcpy(previous, tagId);
Serial.print("Tag value: ");
Serial.println(tagId);
}
else
{
Serial.print("already read");
}
}
I also tried to put the delay after end of tag but it still reads the card multiple times.
I tried another code, it still reads the tag multiple times.
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
int timer=0;
int reference = 1000;
int card_status = 0;
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void read() {
// check, if any data is available
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
timer=0;
}
void loop()
{
if((Rfid.available() > 0 ) && (card_status == 0) )
{
read();
}
if((!Rfid.available() > 0 ) && (card_status == 1) )
{
card_status=0;
}
}
I'm sorry for the late response. I forgot about this topic.
I solved the problem by making the arduino wait for a response after writing the RFID code for the frist time.
I was able to do that because my arduino was sending the code to a C# application via serial port.
Here is how it works: the arduino prints the RFID code on the serial, from there it is picked up by the C# application which searches a database to see if the code is stored there. Depending on the result, the application prints a character('y' or 'n') which is picked up by the arduino. Depending on the character recieved, the arduino lights up a led ( green or red) and makes a noise. Now a new RFID reading can be made.
Here is the code:
#include <SoftwareSerial.h>
#include "RDM6300.h"
SoftwareSerial rdm_serial(8, 9);
RDM6300<SoftwareSerial> rdm(&rdm_serial);
String comanda;
char c="";
int led_verde = 2;
int led_rosu = 7;
int buzzer = 12;
int i;
void buzz(int n = 1)
{
for (int i = 0; i < n; i++) {
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
}
}
void ledVerde()
{
digitalWrite(led_verde, HIGH);
buzz(1);
delay(1000);
digitalWrite(led_verde, LOW);
}
void ledRosu()
{
digitalWrite(led_rosu, HIGH);
buzz(3);
delay(1000);
digitalWrite(led_rosu, LOW);
}
void setup()
{
pinMode(led_verde, OUTPUT);
pinMode(led_rosu, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(led_verde, LOW);
digitalWrite(led_rosu, LOW);
digitalWrite(buzzer, HIGH);
Serial.begin(9600);
}
void loop()
{
static unsigned long long last_id = 0;
last_id = rdm.read();
rdm.print_int64(last_id);
Serial.println();
rdm_serial.end();
Serial.flush();
while(!Serial.available());
c=Serial.read();
if(c=='y')
{
ledVerde();
c="";
}
if(c=='n')
{
ledRosu();
}
Serial.flush();
last_id="";
c="";
rdm_serial.begin(9600);
}
You can find the RDM6300 library here: https://github.com/arliones/RDM6300-Arduino
Long time passed the original question, but maybe my answer would be useful for future visitors.
The RDM6300 works by:
automatically reading the data, and then
your code transfers the read data to the buffer for further processing.
Imagine it as a Baggage carousel in the airport. There are multiple luggage (data) on the carousel (reader), and you pick them one by one (transferring to buffer).
So, the problem of multiple reads, is that you have got read data in the reader (luggage on the carousel), that your code is gradually transferring them to the buffer (picking the luggage up).
In our example, if you don't like to collect all luggage, you can ask someone to take some of them, before they reach to you.
The below code does this. While you have data in the reader (the card is near to the reader), it transfers data from reader to buffer and then zeros all of them in the buffer:
First, place this code before "void setup()":
boolean multipleRead = false;
This defines a false/true variable to tell if this is the first time you are reading the tag (false), or it's being read multiple times (true).
Then, put this one at the end of the code block that shows the tag is fully read. If you are using Michael Schoeffler's library for your RDM6300/630 RFID, put it after "else if (ssvalue == 3) {":
multipleRead = true;
It change the variable to true, when your tag is read. That tells the program that your first read is done and the next upcoming read(s) would be "multiple read" and you don't want them.
Then put this at the end of the RFID reader code (if your RFID code is under void loop (), put the below code just after "void loop (){":
if (multipleRead) {
while (ssrfid.available() > 0) {
int ssvalue = ssrfid.read(); // read
if (ssvalue == -1) { // no data was read
break;
}
}
for (int x = 0; x < 14; x++)
{
buffer[x] = 0;
}
multipleRead = false;
}
It empties the reader and then zeros the buffer, while there is a card nearby. When you move the card away, multipleRead value would turn to false, which let another RFID reading loop to initiate.
Hope that helped :)
I guess what you want is a edge trigger instead of level trigger with time limit.
For example, you may prefer to read a RFID card when it firstly comes near to the antenna, once only; when it keeps to contact the antenna, still take no more actions. When you remove the card and place it again near to the antenna, the MCU starts to read the card again.
If so, the following code could be for your reference. What I have done is just to add 1 more flag to keep checking the card_status before checking if a RFID card comes near to the antenna.
int card_status = 0; //0:readable; 1:not-readable
if ( (Serial.available() > 0) && (card_status == 0) ) {
card_status = 1; //disable to read card after exit this loop
//your code, a card is near to the antenna, try to read it.
readVal = Serial.read();
if (readVal == 2) {
counter = 0; // start reading
} else if (readVal == 3) {
processTag();
clearSerial();
counter = -1;
} else if (counter >= 0) {
readData[counter] = readVal;
++counter;
}
}
reset the card status when no RFID signal comes in && card status is true,
if ( (!(Serial.available() > 0)) && (card_status == 1) ) {
card_status = 0; //enable to read card again
//no signal, no card is near to the antenna.
}
You can set a delay (e.g. delay(2000)) after reading the (end of tag). A delay of (say) 2 seconds will allow the user to move the card far enough away from the reader. Note that delay is a blocking command which might not suit your purposes, in which case you could be looking at using the millis count to activate/deactivate the Serial.read.
Another option is to accept the multiple reads, but keep a state of which card has been read. If the new card number is the same as the old card number (within a reasonable timeframe) then just ignore.

Arduino control with serial input more then 1 character

I have the script below and it works.
But I want to control it with a word like on or off instead of one character.
I tried and searched a lot but without success.
/*
Simple LED sketch
*/
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(115200); // baud rate
}
void loop()
{
if (Serial.available()) {
int ser = Serial.read(); //read serial as ascii integer
if (ser == 'a') { //is this serial byte the ASCII equivalent of 0 through 9?
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (ser == 'u') {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Use Serial.readStringUntil(terminator) to read a string from the serial.
The sent string needs to be terminated with a newline character.
Chose Newline in the Arduino IDE's Serial Monitor.
String cmd = "";
void loop()
{
if (Serial.available()) {
cmd = Serial.readStringUntil('\n');
if (cmd == "on") {
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (cmd == "off") {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Your problem is that you are using a char you need to declare a string so you can compare that string to the input, the guy above gave a ok/very slacky/too complex for you answer (sorry no offense I am just trying to show contrast between a complex and a easy answer not trying to offend you) and partially is because he did not explain what he is doing and because he is doing unnecessary/useless work. There is a function in c++ called Serial.readString(), much easier then the stuff up there. Assuming your level of programming (no offense) from your question here is a quick review on data types:
String = ""
int = integer number {1,2,3,4,5,6,...}
char = '' <- Notice the difference from String = ""
float = floating point number {1.2,4.5,...}
(These are not all of them, there is more like byte and so on but just make sure you know how to use the above first)
/*
Simple LED sketch
*/
int led = 7; // Pin 13
String ser; //Declare the string that is going to store what your are going to
//write in the serial
void setup(){
// Start up serial connection
//It's good convention to start the serial before pinMode or any other thing
//in setup
Serial.begin(9600); //I don't know why you need such an high baud rate in
//your thing, 9600 for what you have to do is more than fine, this is just at
//what speed the serial is read (in a very general explanation)
pinMode(led, OUTPUT); // Set pin 13 as digital out
//make sure you put in here wheter you want to start the led "on" or "off"
//Eg you want it to start on, then digitalWrite(led, HIGH);
}
void loop(){
ser = Serial.readString();
if(Serial.available() == 0) { //You can also use a while loop if you want to
//This is telling the arduino: if there is something in the serial, then do...
if(ser == "on"){
Serial.println("on");
digitalWrite(led, HIGH);
}else if(ser == "off"){
Serial.println("off");
digitalWrite(led, LOW);
}
}
}
Hope it helped!
Also notice how the above code with
if(Serial.available())
This is a quite WEIRD and SHADY statement and might not work. That's because you are not really telling the int value into the function Serial.available
As the arduino guidelines specify:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
viewable here

Serial communication on Arduino

I have an assignment for school where I need to turn on a led with the serial message #ON%, and turn the led off with #OFF%. The # and % are the identifiers for the correct string. So I made this code:
(bericht means message in Dutch)
String readString = "";
int recievedCharacter;
String bericht = "";
int ledPin = 6;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(ledPin, OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
delay(4);
char readChar = (char) Serial.read(); // 'Convert' to needed type
bericht += + readChar; // concatenate char to message
}
if(bericht.startsWith("#"))
{
if(bericht == "#ON%")
{
Serial.println(bericht);
Serial.println("goed");
digitalWrite(ledPin, HIGH);
//message = "";
}
if(bericht == "#OFF%")
{
Serial.println("goed");
digitalWrite(ledPin, LOW);
//message = "";
}
}
}
The problem is the program will never get into the if(bericht == "#ON%") section...
Sorry if this is a silly question but with a lot of googling I just can't figure it out...
The problem is here:
bericht += + readChar; // concatenate char to message // XXX '+ char' => int
this actually appends an integer to the message. Remove the +:
bericht += readChar; // concatenate char to message // Goed!

Arduino Serial Input to Stop and Start

I am trying to wait for user input to start a program operation and then when the user sends a stop command, the loop stops running. I have been unable to get the Serial port to keep reading an input while the loop is running.
So I want the user to Press 1 and then it'll go into the loop and will display the data from the interrupt. But I want it to keep monitoring the Serial Input so when I type in 2, I will get out of the loop and stop printing to the Serial Port.
The serial port isn't registering my second input.
I left out some of the code, but the important stuff should be there.
int userStart = 0; // Holder for user input to start program
int userStop = 0; // Holder for user input to stop program
void setup() {
Serial.begin(115200);
pinMode(motorEncoderA, INPUT);
digitalWrite(motorEncoderA, HIGH); // Pull up resistor
pinMode(motorEncoderB, INPUT);
digitalWrite(motorEncoderB,HIGH); // Pull up resistor
// Interrupt on change of Pin A
attachInterrupt(digitalPinToInterrupt(2), encoderFunc, CHANGE);
Serial.print("Press 1 to start the Process & 2 to Stop");
}
void loop() {
if (Serial.available() > 0)
{
userStart = Serial.read();
if (userStart = 1) {
Serial.print('\n');
while(userStop != 2) {
unsigned long timee = millis();
// Only update if the shaft has moved
if (encoderPositionLast != rotationCounter) {
Serial.print("Time: ");
Serial.print(timee);
Serial.print(" Count: ");
Serial.print (rotationCounter);
Serial.print('\n');
encoderPositionLast = rotationCounter;
Serial.print(userStart);
}
if (Serial.available() > 0) {
userStop = Serial.read();
Serial.print(userStop);
}
}
}
}
Well, I think your problem is that userStart and userStop should not be 1 and 2, but '1' and '2'.
That said, there are some things in your code I dislike.
First of all why is everybody using int as the base type for all numeric variables? If one single byte is enough, use it. On 32bit machines int and byte are almost the same, but on 8bit ones working with ints wastes space and time.
Secondly, I highly discourage you to block the loop function, otherwise you won-t be able to do anything else. Instead, use a variable to track wheter you are running or not, update it with the serial interface, and then execute the code if you are running.
This code should do it. And IMHO it is much better than blocking the loop:
bool running = false;
void setup()
{
...
running = false;
}
void loop()
{
if (Serial.available() > 0)
{
switch(Serial.read())
{
case '1':
running = true;
Serial.print('\n');
break;
case '2':
running = false;
Serial.print("stopped");
break;
}
}
if (running)
{
unsigned long timee = millis();
// Only update if the shaft has moved
if (encoderPositionLast != rotationCounter) {
Serial.print("Time: ");
Serial.print(timee);
Serial.print(" Count: ");
Serial.print (rotationCounter);
Serial.print('\n');
encoderPositionLast = rotationCounter;
Serial.print("running");
}
}
}

Trouble with buttons (Arduino)

StackOverflow! I am a teenager fairly new to Arduino coding, and am investigating a problem. I am running Linux Ubuntu 10.10 if that makes a difference. I have a setup going with a homemade board consisting of five buttons, one for each left, right, up, and down, and one center button, and a Hitachi 2x16 character LCD screen, using the Arduino Uno board.. My programs purpose is to play my splash screen, issue a countdown from three on-screen, display a helpful message, and then go into the program, which simply opens up a list of items, showing one at a time, and allows you to scroll through them using the up and down buttons, and exit the program by clicking the center button. So, here's the problem: the rest of the program works just fine, but when it comes to the menu, it correctly displays the first item, but the buttons do not serve their purpose. I checked using the serial monitor, and the state of the button doesn't change when pressed. But here's the kicker. I used a volt meter to check if the buttons were correctly using power, and they are. They just don't seem to be working digitally. I have checked my wiring, and it is all correct. I think the problem may be in the code, so I leave it to your more experienced hands:
// include the library code:
#include <LiquidCrystal.h>
int itemNum = 0;
// Declare variables for on and off states, for efficiency!
int on = LOW;
int off = HIGH;
// Declare the buttons' digital pins and states respectively
const int upbtnpin = 8;
const int dwnbtnpin = 9;
const int cntrbtnpin = 10;
int upbtnstate = off;
int dwnbtnstate = off;
int cntrbtnstate = off;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// Activate Serial Monitor
Serial.begin(9600);
// Declare buttons to be INPUT
pinMode(upbtnpin, INPUT);
pinMode(dwnbtnpin, INPUT);
pinMode(cntrbtnpin, INPUT);
// Activate Pullups
digitalWrite(upbtnpin, HIGH);
digitalWrite(dwnbtnpin, HIGH);
digitalWrite(cntrbtnpin, HIGH);
// Begin LCD
lcd.begin(16, 2);
// Splash Screen
lcd.setCursor(0, 0);
lcd.print(" _# Paradigm #_");
lcd.setCursor(0,1);
lcd.print("<-> Robotics <->");
delay(3000);
lcd.clear();
// Display Message and countdown
cntrbtnstate = digitalRead(cntrbtnpin);
lcd.setCursor(0, 0);
lcd.print("Menu Program 1.0");
lcd.setCursor(0, 1);
int i = 3;
while (i > 0) {
if (i == 3) {
lcd.setCursor(0, 1);
lcd.print("Wait 3 second(s)");
delay(1000);
i--;
}
else if (i == 2) {
lcd.setCursor(0, 1);
lcd.print("Wait 2 second(s)");
delay(1000);
i--;
}
else if (i == 1) {
lcd.setCursor(0, 1);
lcd.print("Wait 1 second(s)");
delay(1000);
i--;
}
}
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print("Press enter any");
lcd.setCursor(0, 1);
lcd.print(" time to exit. ");
delay(1200);
lcd.clear();
}
// ...And finally, to the actual program!
void loop() {
upbtnstate = digitalRead(upbtnpin);
dwnbtnstate = digitalRead(dwnbtnpin);
cntrbtnstate = digitalRead(cntrbtnpin);
while (cntrbtnstate != on) {
if (upbtnstate == on) {
delay(250);
itemNum++;
}
else if (dwnbtnstate == on) {
delay(250);
itemNum--;
}
// OPTIONAL for debugging
delay(150);
Serial.print(" ! ");
delay(150);
Serial.print(int(upbtnstate));
Serial.print(" ");
delay(150);
Serial.print(int(dwnbtnstate));
Serial.print(" ");
delay(150);
Serial.print(int(cntrbtnstate));
String item = "";
int itemNum = 0;
switch(itemNum) {
case 0:
lcd.setCursor(0, 1);
lcd.print("END OF LIST");
break;
case 1:
lcd.setCursor(0, 1);
lcd.print("Water Bottle");
break;
case 2:
lcd.setCursor(0, 1);
lcd.print("Biker Jacket");
break;
case 3:
lcd.setCursor(0, 1);
lcd.print(".44 Magnum");
break;
case 4:
lcd.setCursor(0, 1);
lcd.print(".44 Rounds (x14)");
break;
case 5:
lcd.setCursor(0, 1);
lcd.print("Machete");
break;
case 6:
lcd.setCursor(0, 1);
lcd.print("END OF LIST");
break;
default:
lcd.setCursor(0, 0);
lcd.print(" An error has ");
lcd.setCursor(0, 1);
lcd.print(" occoured ");
}
if (itemNum > 6) {
itemNum == 6;
}
else if (itemNum < 0) {
itemNum == 0;
}
lcd.setCursor(0, 1);
lcd.print(item);
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Goodbye! ");
}
First, you need to de-bounce the buttons.
http://arduino.cc/it/Tutorial/Debounce
Second, immediately before your case statement, you set the itemNum to 0. That should probably be moved to the setup section, otherwise it will reset the itemNum each iteration of the loop, and you will always be stuck on "END OF LIST".
Likewise with String item = ""; You are blanking out the string each iteration of the loop, and it runs faster than you can press the buttons.
Also, I don't see any code that ever sets the String item to anything except "". Since you print the item name in the case statement, perhaps you can just remove "String item" and all references.
Finally, you can shorten the delay loop section to :
int i = 3;
while (i > 0) {
lcd.setCursor(0, 1);
lcd.print("Wait " + i + " second(s)");
delay(1000);
i--;
}

Resources