I am creating a morse code generator that uses an lcd display, 2 push buttons and and led to flash the corresponding code. I need one push button to count down through the alphabet while the other goes back through the alphabet and if nothing is pushed it automatically continues to count down through the alphabet below is the code i have so far. I just cant figure out how to incorporate the push buttons correctly. any help would be greatly appreciated.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int LED_Pin = 13; // Set Pin 13 as the output pin where the LED is connected
const int buttonPin = 8; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 7; // the pin that the Down pushbutton is attached to
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState8 = 0; // current state of the button
int buttonState7 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(LED_Pin, OUTPUT);
}
void loop() {
lcd.clear();
buttonState8 = digitalRead(buttonPin); // read the pushbutton up input pin:
// compare the buttonState to its previous state
if (buttonState8 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState8 == HIGH){
buttonPushCounter++;}
}
lastButtonState = buttonState8;
// read the pushbutton down input pin:
buttonState7 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState7 != lastButtonState) {
if (buttonState7 == HIGH){
buttonPushCounter-=1; }
}
lastButtonState = buttonState7;
if (buttonPushCounter =0){
lcd.print("A"); delay(1000);
dot(); dash(); delay(1000); // Letter A
}
if (buttonPushCounter =1){
lcd.print("B"); delay(1000);
dash(); dot(); dot(); dot(); delay(1000); // Letter B
}
if (buttonPushCounter =2){
lcd.print("C"); delay(1000);
dash(); dot(); dash(); dot(); delay(1000); // Letter C
}
if (buttonPushCounter =3){
lcd.print("D"); delay(1000);
dash(); dot(); dot(); delay(1000); // Letter D
}
if (buttonPushCounter =4){
lcd.print("E"); delay(1000);
dot(); delay(1000); // Letter E
}
if (buttonPushCounter =5){
lcd.print("F"); delay(1000);
dot(); dot(); dash(); dot(); delay(1000); // Letter F
}
if (buttonPushCounter =6){
lcd.print("G"); delay(1000);
dash(); dash(); dot(); delay(1000); // Letter G
}
if (buttonPushCounter =7){
lcd.print("H"); delay(1000);
dot(); dot(); dot(); dot(); delay(1000); // Letter H
}
if (buttonPushCounter =8){
lcd.print("I"); delay(1000);
dot(); dot(); delay(1000); // Letter I
}
if (buttonPushCounter =9){
lcd.print("J"); delay(1000);
dot(); dash(); dash(); dash(); delay(1000); // Letter J
}
if (buttonPushCounter =10){
lcd.print("K"); delay(1000);
dash(); dot(); dash(); delay(1000); // Letter K
}
if (buttonPushCounter =11){
lcd.print("L"); delay(1000);
dot(); dash(); dot(); dot(); delay(1000); // Letter L
}
if (buttonPushCounter =12){
lcd.print("M"); delay(1000);
dash(); dash(); delay(1000); // Letter M
}
if (buttonPushCounter =13){
lcd.print("N"); delay(1000);
dash(); dot(); delay(1000); // Letter N
}
if (buttonPushCounter =14){
lcd.print("O"); delay(1000);
dash(); dash(); dash(); delay(1000); // Letter O
}
if (buttonPushCounter =15){
lcd.print("P"); delay(1000);
dot(); dash(); dash(); dot(); delay(1000); // Letter P
lcd.clear();
}
if (buttonPushCounter =16){
lcd.print("Q"); delay(1000);
dash(); dash(); dot(); dash(); delay(1000); // Letter Q
}
if (buttonPushCounter =17){
lcd.print("R"); delay(1000);
dot(); dash(); dot(); delay(1000); // Letter R
}
if (buttonPushCounter =18){
lcd.print("S"); delay(1000);
dot(); dot(); dot(); delay(1000); // Letter S
}
if (buttonPushCounter =19){
lcd.print("T"); delay(1000);
dash(); delay(1000); // Letter T
}
if (buttonPushCounter =20){
lcd.print("U"); delay(1000);
dot(); dot(); dash(); delay(1000); // Letter U
}
if (buttonPushCounter =21){
lcd.print("V"); delay(1000);
dot(); dot(); dot(); dash(); delay(1000); // Letter V
}
if (buttonPushCounter =22){
lcd.print("W"); delay(1000);
dot(); dash(); dash(); delay(1000); // Letter W
}
if (buttonPushCounter =23){
lcd.print("X"); delay(1000);
dash(); dot(); dot(); dash(); delay(1000); // Letter X
}
if (buttonPushCounter =24){
lcd.print("Y"); delay(1000);
dash(); dot(); dash(); dash(); delay(1000); // Letter Y
}
if (buttonPushCounter =25){
lcd.print("Z"); delay(1000);
dash(); dash(); dot(); dot(); delay(1000); // Letter Z
lcd.clear();
}
}
void dot()
{
digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(150); // Leave it on for 0.15 seconds
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(150); // Leave it off for 0.10 seconds
}
void dash()
{
digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(500); // Leave it on for 0.5 seconds (Longer because it is a dash)
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(150); // Spacing between dashes
}
You have a variable for the up button state and one for the down button state yet you only have one last button state variable. Does that strike you as odd?
The code is appallingly written and so is way way longer than it needs to be.
I am just a beginner so I do not know another way to code it to make the coding shorter. What do you mean about the last button state variable? I was using the last button state variable to update the buttonstate8 and buttonstate 7. I have modified the code slightly and it is below.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int LED_Pin = 13; // Set Pin 13 as the output pin where the LED is connected
const int buttonPin = 8; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 7; // the pin that the Down pushbutton is attached to
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState8 = 0; // current state of the button
int buttonState7 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(LED_Pin, OUTPUT);
}
void loop() {
buttonState8 = digitalRead(buttonPin); // read the pushbutton up input pin:
// compare the buttonState to its previous state
if (buttonState8 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState8 == HIGH){
buttonPushCounter++;}
}
lastButtonState = buttonState8;
// read the pushbutton down input pin:
buttonState7 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState7 != lastButtonState) {
if (buttonState7 == HIGH){
buttonPushCounter-=1; }
}
lastButtonState = buttonState7;
if (buttonPushCounter ==1){
lcd.print("A");
dot(); dash(); delay(1000); // Letter A
lcd.clear();
}
if (buttonPushCounter ==2){
lcd.print("B");
dash(); dot(); dot(); dot(); delay(1000); // Letter B
lcd.clear();
}
if (buttonPushCounter ==3){
lcd.print("C");
dash(); dot(); dash(); dot(); delay(1000); // Letter C
lcd.clear();
}
if (buttonPushCounter ==4){
lcd.print("D");
dash(); dot(); dot(); delay(1000); // Letter D
lcd.clear();}
if (buttonPushCounter ==5){
lcd.print("E");
dot(); delay(1000); // Letter E
lcd.clear();}
if (buttonPushCounter ==6){
lcd.print("F");
dot(); dot(); dash(); dot(); delay(1000); // Letter F
lcd.clear();}
if (buttonPushCounter ==7){
lcd.print("G");
dash(); dash(); dot(); delay(1000); // Letter G
lcd.clear();}
if (buttonPushCounter ==8){
lcd.print("H");
dot(); dot(); dot(); dot(); delay(1000); // Letter H
lcd.clear();}
if (buttonPushCounter ==9){
lcd.print("I");
dot(); dot(); delay(1000); // Letter I
lcd.clear();}
if (buttonPushCounter ==10){
lcd.print("J");
dot(); dash(); dash(); dash(); delay(1000); // Letter J
lcd.clear();}
if (buttonPushCounter ==11){
lcd.print("K");
dash(); dot(); dash(); delay(1000); // Letter K
lcd.clear();}
if (buttonPushCounter ==12){
lcd.print("L");
dot(); dash(); dot(); dot(); delay(1000); // Letter L
lcd.clear();}
if (buttonPushCounter ==13){
lcd.print("M");
dash(); dash(); delay(1000); // Letter M
lcd.clear();}
if (buttonPushCounter ==14){
lcd.print("N");
dash(); dot(); delay(1000); // Letter N
lcd.clear();}
if (buttonPushCounter ==15){
lcd.print("O");
dash(); dash(); dash(); delay(1000); // Letter O
lcd.clear();}
if (buttonPushCounter ==16){
lcd.print("P"); delay(1000);
dot(); dash(); dash(); dot(); delay(1000); // Letter P
lcd.clear();
}
if (buttonPushCounter ==17){
lcd.print("Q");
dash(); dash(); dot(); dash(); delay(1000); // Letter Q
lcd.clear();}
if (buttonPushCounter ==18){
lcd.print("R");
dot(); dash(); dot(); delay(1000); // Letter R
lcd.clear();}
if (buttonPushCounter ==19){
lcd.print("S");
dot(); dot(); dot(); delay(1000); // Letter S
lcd.clear();}
if (buttonPushCounter ==20){
lcd.print("T");
dash(); delay(1000); // Letter T
lcd.clear();}
if (buttonPushCounter ==21){
lcd.print("U");
dot(); dot(); dash(); delay(1000); // Letter U
lcd.clear();}
if (buttonPushCounter ==22){
lcd.print("V");
dot(); dot(); dot(); dash(); delay(1000); // Letter V
lcd.clear();}
if (buttonPushCounter ==23){
lcd.print("W");
dot(); dash(); dash(); delay(1000); // Letter W
lcd.clear();}
if (buttonPushCounter ==24){
lcd.print("X");
dash(); dot(); dot(); dash(); delay(1000); // Letter X
lcd.clear();}
if (buttonPushCounter ==25){
lcd.print("Y");
dash(); dot(); dash(); dash(); delay(1000); // Letter Y
lcd.clear();}
if (buttonPushCounter ==26){
lcd.print("Z");
dash(); dash(); dot(); dot(); delay(1000); // Letter Z
lcd.clear(); }
}
void dot()
{ digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(150); // Leave it on for 0.15 seconds
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(100); // Spacing}
void dash()
{ digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(500); // Leave it on for 0.5 seconds (Longer because it is a dash)
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(100); // Spacing between dashes}
What do you mean about the last button state variable? I was using the last button state variable to update the buttonstate8 and buttonstate 7
What I mean, and was trying to get you to see, is that you need a last state variable for each of the buttons not one shared between the two.
You were asked to post you code using code tags and you have not. We take that as a sign of discourtesy here.
Please read this:- How to use this forum
Because your post is breaking the rules about posting code.
Sorry! I understand the (</>) now. Is this what you mean adding another variable? I feel like the way I coded it is the same just with one more variable.
// compare the buttonState to its previous state
if (buttonState8 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState8 == HIGH){
buttonPushCounter++;}
}
lastButtonState = buttonState8;
lastButtonState1= buttonState8;
// read the pushbutton down input pin:
buttonState7 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState7 != lastButtonState1) {
if (buttonState7 == HIGH){
buttonPushCounter-=1; }
}
lastButtonState1 = buttonState7;
lastButtonState = buttonState7;
if (buttonPushCounter ==0){
buttonPushCounter = 26;}
if (buttonPushCounter ==1){
lcd.print("A");
dot(); dash(); delay(800); // Letter A
lcd.clear();}
if (buttonPushCounter ==2){
lcd.print("B");
dash(); dot(); dot(); dot(); delay(800); // Letter B
lcd.clear();}
if (buttonPushCounter ==3){
lcd.print("C");
dash(); dot(); dash(); dot(); delay(800); // Letter C
lcd.clear();}
if (buttonPushCounter ==4){
lcd.print("D");
dash(); dot(); dot(); delay(800); // Letter D
lcd.clear();}
if (buttonPushCounter ==5){
lcd.print("E");
dot(); delay(800); // Letter E
lcd.clear();}
if (buttonPushCounter ==6){
lcd.print("F");
dot(); dot(); dash(); dot(); delay(800); // Letter F
lcd.clear();}
if (buttonPushCounter ==7){
lcd.print("G");
dash(); dash(); dot(); delay(800); // Letter G
lcd.clear();}
if (buttonPushCounter ==8){
lcd.print("H");
dot(); dot(); dot(); dot(); delay(800); // Letter H
lcd.clear();}
if (buttonPushCounter ==9){
lcd.print("I");
dot(); dot(); delay(800); // Letter I
lcd.clear();}
if (buttonPushCounter ==10){
lcd.print("J");
dot(); dash(); dash(); dash(); delay(800); // Letter J
lcd.clear();}
if (buttonPushCounter ==11){
lcd.print("K");
dash(); dot(); dash(); delay(800); // Letter K
lcd.clear();}
if (buttonPushCounter ==12){
lcd.print("L");
dot(); dash(); dot(); dot(); delay(800); // Letter L
lcd.clear();}
if (buttonPushCounter ==13){
lcd.print("M");
dash(); dash(); delay(800); // Letter M
lcd.clear();}
if (buttonPushCounter ==14){
lcd.print("N");
dash(); dot(); delay(800); // Letter N
lcd.clear();}
if (buttonPushCounter ==15){
lcd.print("O");
dash(); dash(); dash(); delay(800); // Letter O
lcd.clear();}
if (buttonPushCounter ==16){
lcd.print("P");
dot(); dash(); dash(); dot(); delay(800); // Letter P
lcd.clear();}
if (buttonPushCounter ==17){
lcd.print("Q");
dash(); dash(); dot(); dash(); delay(800); // Letter Q
lcd.clear();}
if (buttonPushCounter ==18){
lcd.print("R");
dot(); dash(); dot(); delay(800); // Letter R
lcd.clear();}
if (buttonPushCounter ==19){
lcd.print("S");
dot(); dot(); dot(); delay(800); // Letter S
lcd.clear();}
if (buttonPushCounter ==20){
lcd.print("T");
dash(); delay(800); // Letter T
lcd.clear();}
if (buttonPushCounter ==21){
lcd.print("U");
dot(); dot(); dash(); delay(800); // Letter U
lcd.clear();}
if (buttonPushCounter ==22){
lcd.print("V");
dot(); dot(); dot(); dash(); delay(800); // Letter V
lcd.clear();}
if (buttonPushCounter ==23){
lcd.print("W");
dot(); dash(); dash(); delay(800); // Letter W
lcd.clear();}
if (buttonPushCounter ==24){
lcd.print("X");
dash(); dot(); dot(); dash(); delay(800); // Letter X
lcd.clear();}
if (buttonPushCounter ==25){
lcd.print("Y");
dash(); dot(); dash(); dash(); delay(800); // Letter Y
lcd.clear();}
if (buttonPushCounter ==26){
lcd.print("Z");
dash(); dash(); dot(); dot(); delay(800); // Letter Z
lcd.clear();}
if (buttonPushCounter ==27){
buttonPushCounter = 1;}
}
void dot()
{
digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(150); // Leave it on for 0.15 seconds
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(100); // Spacing
}
void dash()
{
digitalWrite(LED_Pin, HIGH); // Turn on the LED
delay(500); // Leave it on for 0.5 seconds (Longer because it is a dash)
digitalWrite(LED_Pin, LOW); // Turn the LED off
delay(100); // Spacing between dashes
}
I corrected the code and I believe this is what you were trying to get at.
buttonState8 = digitalRead(buttonPin); // read the pushbutton up input pin:
// compare the buttonState to its previous state
if (buttonState8 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState8 == HIGH){
buttonPushCounter++;}
}
lastButtonState = buttonState8;
// read the pushbutton down input pin:
buttonState7 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState7 != lastButtonState1) {
if (buttonState7 == HIGH){
buttonPushCounter-=1; }
}
lastButtonState1 = buttonState7;
Yes that is right, that is what I was getting at. However I was expecting better variable names like pairing buttonState8 with lastButtonState8 so it is easy to see how they relate.