Hello,
I am relatively new to arduinos and coding, and I am struggling with figuring out how to get my project to count properly and display the number of cycles on the LCD display. Essentially what I am doing is using an Arduino UNO to power a SPDT momentary switch, and count the number of cycles. I have gotten the switch and everything to work (thanks to the wonderful internet) and I have gotten the LCD to power on and display a simple line. However, I want it to count when either side of the switch is activated, so when the left side is closed, thats one cycle, and when the other side is closed, thats another cycle. I'm sure whatever it takes to fix my code is super simple, but I have been out of the programming game for awhile. Any help is appreciated.
sketch_oct29a.ino (1.2 KB)
OP's code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;
int e = 9;
int i = 10;
void setup() {
// put your setup code here, to run once:
pinMode (6, INPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("# of Cycles");
pinMode(e,INPUT);
pinMode(i,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int switchValue = digitalRead(6);
if (switchValue == HIGH) {
digitalWrite (7, HIGH);
digitalWrite (8, LOW);
}
// else if (switchValue == LOW) {
// digitalWrite (7, LOW);
// digitalWrite (8, LOW);
// }
else{
digitalWrite (7, LOW);
digitalWrite (8, HIGH);
}
// int switchValue = digitalRead(e);
// int switchValue = digitalRead(10);
lcd.setCursor(3, 1);
lcd.print(a);
if (pinMode(e,INPUT)) {
a ++;
lcd.setCursor(3, 1);
lcd.print(a);
delay(200);
}
// else if (pinMode(i,INPUT) == HIGH) {
// a ++;
// lcd.setCursor(3, 1);
// lcd.print(a);
// delay(200);
// }
else {
lcd.setCursor(3, 1);
lcd.print(a);
delay(200);
}
}
if (pinMode(e,INPUT)) {
Don't you get a compilation error for that?
Yes. The error message displayed is:
"exit status 1
could not convert 'pinMode(((uint8_t)e), 0)' from 'void' to 'bool' "
So, why are you trying to test the value that the function doesn't return?
What did you intend?
There are four issues:
-
To read value from Button, Use digitalRead() function instead of pinMode()
-
You SHOULD use pull-up or pin-down resistor. See Arduino Button Pullup/Pull-down Tutorial
-
For counting, you SHOULD avoid chattering phenomenon. See Arduino Button Bebounce Tutorial
4, You SHOULD give the variable name that is understandable. For example: int i = 10; => int buttonPin = 10;