I am trying to make a project where a number is sent to serial via a python program. Then an Arduino is to read the number and perform a specific function (here printing something on an lcd) for a specified duration. However the code doesn't work as intended.
Here is the code running on the Arduino:
#include <LiquidCrystal.h>
boolean flag1, flag2;
const int rs = 3, en = 2, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
String a;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("hello");
delay(1000);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
if(Serial.available())
{
int c = Serial.read();
if(c == 49) { // 1 is pressed
flag1 = true;
flag2 = false;
}
else if(c == 50) { // 2 is pressed
flag2 = true;
flag1 = false;
}
}
if(flag1) {
lcd.write("location 1");
delay(3000);
lcd.clear();
}
if(flag2) {
lcd.write("location 2");
delay(3000);
lcd.clear();
}
delay(50);
}
When the number is pressed it shows the text however it keeps it on the screen forever. I need it to run once (for the specified delay duration) then reset to blank screen. When another number is entered, it changes the text but again keeps it on forever.
Clear the flag after reading it:
if(flag1) {
flag1 = false;
// ...
Before you send your first characters, the if statements that write to your LCD are already being evaluated. Unfortunately, your flags haven't been initialized. I would suggest:
boolean flag1 = false, flag2 = false;
at the top of the file.
Related
I am having trouble with a 16x2 I2C LCD screen using a 4x3 keyboard.
The following are the steps I need to do:
1.Take a numeric value through the keyboard onto the top line of the screen. This numeric value should not exceed 3 digits.
2. The keyboard should only be active (and the screen editable) when * key is pressed once.
3. The keyboard should again become inactive when # key is pressed once.
4. Whatever value has been placed in the screen should be remembered so when a sensor runs it should be able to compare its value to the screen value, achieving equality to which the core process stops.
Below is my code which is not doing the job. Can someone suggest a solution? Thanks.
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
#include<stdio.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5};
byte pin_column[COLUMN_NUM] = {4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
int counter = 0;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Volume. ml");
lcd.setCursor(0, 1);
lcd.print("Filled ml");
}
void loop() {
// put your main code here, to run repeatedly:
keypadfunction();
}
void keypadfunction()
{
char key = keypad.getKey();
if (key)
{
}
if (key == '*')
{
while (key != '#' || counter <=3)
{
lcd.setCursor(8, 1);
lcd.setCursor(8+counter, 1);
lcd.print(key);
counter = counter+1;
}
}
}
changes after suggestions from #C1sc0:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
#include<stdio.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5};
byte pin_column[COLUMN_NUM] = {4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column,
ROW_NUM, COLUMN_NUM );
char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;
void setup()
{
// put your setup code here, to run once:
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Volume ml");
lcd.setCursor(0, 1);
lcd.print("Filled ml");
}
void loop()
{
char key = keypad.getKey();
if(key) // check if any key was pressed
{
if(key == '*') // if * was pressed switch to edit mode
{
lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
lcd.clear();
lcd.print("Enter volume: ");
edit = true;
lcd.setCursor(0,1); // set your cursor to second row
volumeCount = 0;
volume[0] = '0';
volume[1] = '0';
volume[2] = '0';
}
if(edit && volumeCount < 3) // enter edit mode
{
volume[volumeCount] = key; // save key to a char array
lcd.setCursor(volumeCount,1); // set your cursor to the
next position
lcd.print(volume[volumeCount]); // print the pressed button to lcd
volumeCount++; // increment the array index (cursor position)
}
if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
{
edit = false; // disable edit mode
volumeCount = 0; // reset your counter
lcd.setCursor(0,0);
lcd.clear();//new
lcd.print(volume);
finalValue = atoi(volume); // save your entered value for further usage
//volume[0] = '0';
//volume[1] = '0';
//volume[2] = '0';
}
}
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH)
{
//do process basis 'finalValue'
}
}
Screen output
Create a bool (or byte) variable, for signaling the "edit" state
if (key == '*') { edit= true; }
in your loop (this will be your first if condition).
Create another if and here check the edit flag (if it is true, then you are waiting for the 3 values. You can read the pressed keys to a char array
char values[3];
// create this variable, before setup()
Here for the index you can use a counter (which is already present in your code as I saw. So, put all these lines in your second if condition, and increment your counter by one here. And you can display the entered character here too:
lcd.setCursor(8+counter, 1);
lcd.print(key);
Create a third if block, and check the counter is # key or counter == 3. If this condition will be satisfied you have to do 3 things reset the counter to zero, reset the edit state to false, and finally convert your value to an int (with atoi function).
After it works, you can refactor your code to make it more efficient/readable.
You need something like this:
char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;
void loop()
{
char key = keypad.getKey();
if(key) // check if any key was pressed
{
if(key == '*') // if * was pressed switch to edit mode
{
lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
lcd.print("Enter volume: ");
edit = true;
lcd.setCursor(0,1); // set your cursor to second row
volumeCount = 0;
volume[0] = '0';
volume[1] = '0';
volume[2] = '0';
}
if(edit && volumeCount < 3 && key != '*' && key != '#')
{
volume[volumeCount] = key; // save key to a char array
lcd.setCursor(volumeCount,1); // set your cursor to the next position
lcd.print(volume[volumeCount]); // print the pressed button to lcd
volumeCount++; // increment the array index (cursor position)
}
if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
{
edit = false; // disable edit mode
volumeCount = 0; // reset your counter
finalValue = atoi(volume); // save your entered value for further usage
volume[0] = '0';
volume[1] = '0';
volume[2] = '0'; # ty for the fix!!!
}
}
}
I'm working on a project on which I'm using a keypad to type a password, what I've done is that I'm reading keys typed by the user and collect it in an array to compare it with the password. The problem that I'm facing is that when I compare the typed word and and the correct password I'm getting always "wrong password".
Here is my code:
#include "Keypad.h"
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
char passwrd[7];
char cst[7]="*1998#";
byte rowPins[ROWS] = {28, 27, 26, 25}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {24, 23, 22}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
}
void loop()
{
int i=0;
do
{
char key = keypad.getKey();
if (key != NO_KEY)
{
passwrd[i]=key;
i++;
Serial.println(key);
}
}while (i!=6);
Serial.println(passwrd);
Serial.println(cst);
if (passwrd==cst)
{
Serial.println("correct passwrd");
}
else
{
Serial.println("wrong passwrd");
}
}
Here is what I'm getting from the serial com:
*
1
9
9
8
#
*1998#
*1998#
wrong passwrd
Where is the problem?
Using == on a char* will compare the address that the pointer points to in memory, since a c-type string is a pointer. You need to use the strcmp() function.
strcmp() returns 0 if the c-strings are identical.
This should work:
if (strcmp(passwrd, cst) == 0)
{
Serial.println("correct passwrd");
}
else
{
Serial.println("wrong passwrd");
}
Demonstration
Put this on your Arduino to demonstrate this:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
char* are_the_same(int val) {
if(val == 0)
return "No";
return "Yes";
}
void loop() {
// put your main code here, to run repeatedly:
char* one = "test";
char two[5];
// We are copying 'test' into string two. If we don't do this the compiler will optimise and make them point to the same piece of memory and ruin the demonstration.
int i;
for (i = 0; i < 5; i++)
two[i] = one[i];
Serial.print("one == two, are they the same? ");
Serial.println(are_the_same(one == two));
Serial.print("strcmp(one, two) == 0, are they the same? ");
Serial.println(are_the_same(strcmp(one, two) == 0));
Serial.println();
delay(1000);
}
This will give you:
one == two, are they the same? No
strcmp(one, two) == 0, are they the same? Yes
I have this code which I try to process is entering a password "78486" to shut down the sensor function !
The problem is, when I enter the code using the keypad .. It doesn't work ..
So please help me to make it work .. I think the problem is in the if condition !
#include <Keypad.h>
char* secretCode = "78486";
int position = 0;
int minSecsBetweenEmails = 60; // 1 min
int val = 0;
long lastSend = -minSecsBetweenEmails * 1000l;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {8, 7, 6, 5};
byte colPins[cols] = {4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys),
rowPins, colPins,
rows, cols);
void setup()
{
pinMode (9, OUTPUT);
Serial.begin(9600);
}
void loop()
{
long now = millis();
char key = keypad.getKey();
if (key == '*') {
pinMode (9, INPUT); }
if(key == '2') {
pinMode (9, OUTPUT);
// if(key == secretCode[position])
// {
// position++;
// }
// if (position == 5){
// pinMode (9, OUTPUT);
// }
}
if(digitalRead(9) == HIGH)
{
if (now > (lastSend + minSecsBetweenEmails * 1000l))
{
Serial.println("MOVEMENT");
lastSend = now;
}
else
{
Serial.println("Too soon");
}
}
}
Personally I'd check the value at the end of the reading phase: this way it's harder to guess what is the wrong char (if it exits every time a char is wrong, you will have to do at most 50 tries; if it exits at the end you will need to do at most 100000).
Moreover you need a way to reset the position and start a new password: since you didn't use the # char I used it.
Lastly I definitely prefer the switch case in this case. So
// In the main definition part:
uint8_t position = 0;
char readingCode[5];
char secretCode[] = "78486";
// In the loop:
char key = keypad.getKey();
switch (key)
{
case '*': // shut down sensor
pinMode (9, INPUT);
break;
case '#':
position = 0;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (position < 5)
{
readingCode[position] = key;
position++;
}
if (position >= 5)
{ // check the code
bool correct = true;
uint8_t i;
for (i = 0; i < 5; i++)
if (readingCode[i] != secretCode[i])
correct = false;
if (correct)
pinMode (9, OUTPUT);
position = 0;
}
break;
}
Just one more thing: I don't know what does getKey return when no keys are pressed. Just verify it
I have an application where I am using a MCP3421 18bit ADC to read analog data. The setup is Xbee+Xbee Sheild+Arduino + MCP3421 as Transmitter. This I am reading and transmitting to a remote xbee+arduino module with LCD. The data is displayed fine on the LCD. however I want to receive the data on the Serial port. When i try tp Do a Serial.println(s); on the receiving code the data which i get on serial port is garbled. Would appreciate any help
Here is my Code
Transmitting
#include <Wire.h>
#include <LiquidCrystal.h>
#define TRUE 1
#define FALSE 0
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(void)
{
Serial.begin(9600);
Wire.begin();
delay(100);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>"); // just to be sure things are working
lcd.begin(16, 2);
}
void loop(void)
{
byte address, Hi, Lo, Config;
int ADVal;
while(1)
{
address = 0x68;
Wire.beginTransmission(address);
Wire.write(0x88); // config register %1000 1000
// /RDY = 1, One Conversion, 15 samples per, PGA = X1
Wire.endTransmission();
delay(1);
Wire.requestFrom((int)address, (int) 3);
Hi = Wire.read();
Lo = Wire.read();
Config = Wire.read();
Wire.endTransmission();
ADVal = Hi;
ADVal = ADVal * 256 + Lo;
// Serial.print(ADVal, DEC);
//Serial.print(" ");
//Serial.println(Config, DEC);
Serial.print("<");
Serial.print(ADVal);
Serial.print(">");
//lcd.setCursor(0,0);
//lcd.print(ADVal);
lcd.setCursor(0,1);
//float val = ADVal * 0.00006244087;
//lcd.print(val,3);
delay(1);
}
}
and this is the receiving code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
bool started = false;
bool ended= false;
char inData[10]; // Leave plenty of room
byte index;
float i;
//char inData[24]; // Or whatever size you need
//byte index = 0;
void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop()
{
//Serial.println(s);
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
// Start of packet marker read
index = 0;
inData[index] = '\0'; // Throw away any incomplete packet
started = true;
ended = false;
}
else if(aChar == '>')
{
// End of packet marker read
ended = true;
break; // Done reading serial data for now
}
else
{
if(index < 10) // Make sure there is room
{
inData[index] = aChar; // Add char to array
index++;
inData[index] = '\0'; // Add NULL to end
}
}
}
// When we get here, there is no more serial data to read,
// or we have read an end-of-packet marker
if(started && ended)
{
// We've seen both markers - do something with the data here
lcd.setCursor(0,0);
i = atoi (inData);
float s = (i * 0.3051851); //multiplying with calibration factor
float value = ( s / 1000 );
lcd.setCursor(1,0);
lcd.print(value,3); // print value after multiplying with calibration factor to LCD
lcd.setCursor(0,1 );
lcd.print(i); // Print raw ADC counts as recieved from transmitter
index = 0;
inData[index] = '\0';
started = false;
ended = false;
}
}
The receiving arduino do get the data through Xbee and it displays values perfectly on the LCD( Attached PIC). I also need to receive the data on a PC attached to the receiving arduino through its USB/Serial port.
When i try to use the serial monitor the display on LCD vanishes and the serial monitor displays garbled values. I thing the Serial.print(s) is sending back the data to the XBEE as both the DO and DI LED starts blinking on the XBEE SHIELD.
I'm using an Arduino Micro to trigger specific events on a front end I have. For some reason, however, some of the key combos are just triggering randomly. I am not even touching the arduino when this happens.
I have it set up so when you press a button, it only triggers the key stroke once, rather than repeating it while the button is pressed.
Am I missing something, or screw something up?
int escButton = 12, valEsc, valEsc2, stateEsc;
int volUp = 11, valVup, valVup2, stateVup;
int volDown = 10, valVdown, valVdown2, stateVdown;
int rebootButton = 9, valReboot, valReboot2, stateReboot;
char ctrlKey = KEY_LEFT_GUI;
char shiftKey = KEY_LEFT_SHIFT;
void pressKeyCombos(int combo){
switch(combo) {
case 1:
Keyboard.press(KEY_ESC);
delay(100);
Keyboard.releaseAll();
break;
case 2:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F1);
delay(100);
Keyboard.releaseAll();
break;
case 3:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F3);
delay(100);
Keyboard.releaseAll();
break;
case 4:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F2);
delay(100);
Keyboard.releaseAll();
break;
}
}
void setup() {
pinMode(escButton, INPUT);
stateEsc = digitalRead(escButton);
Keyboard.begin();
}
void loop(){
valEsc = digitalRead(escButton);
delay(10);
valEsc2 = digitalRead(escButton);
valVup = digitalRead(volUp);
delay(10);
valVup2 = digitalRead(volUp);
valVdown = digitalRead(volDown);
delay(10);
valVdown2 = digitalRead(volDown);
valReboot = digitalRead(rebootButton);
delay(10);
valReboot2 = digitalRead(rebootButton);
if (valVup == valVup2) {
if (valVup != stateVup) {
if (valVup == LOW) {
pressKeyCombos(4);
}
}
stateVup = valVup;
}
if (valVdown == valVdown2) {
if (valVdown != stateVdown) {
if (valVdown == LOW) {
pressKeyCombos(3);
}
}
stateVdown = valVdown;
}
if (valReboot == valReboot2) {
if (valReboot != stateReboot) {
if (valReboot == LOW) {
pressKeyCombos(2);
}
}
stateReboot = valReboot;
}
if (valEsc == valEsc2) {
if (valEsc != stateEsc) {
if (valEsc == LOW) {
pressKeyCombos(1);
}
}
stateEsc = valEsc;
}
}
The inputs are high impedance by default. From your description of the beahvior, I can guess that you have not added external pull up/down resistor and that the pins are connected to a normally open switch. The random input would be just as expected and noted in docs for digitialRead():
Note
If the pin isn't connected to anything, digitalRead() can return
either HIGH or LOW (and this can change randomly).
see reference page for digitialRead()
The micro does have the ability to provide an internal pull up.
Your code does not show you have enabled the internal pull up resistors. This is accomplished in a manner that may seem odd, by writing to the input pin. From the Arduino docs:
There are also convenient 20K pullup resistors built into the Atmega
chip that can be accessed from software. These built-in pullup
resistors are accessed in the following manner.
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors
see Digital Pins
So in your case, add four digitalWrite()'s in the setup() to enable the pull ups and you look good to go.