I have read the begginer's guide already, actually I've been looking at turorials and sketches for the past two weeks. I have a code for a counter which I looked up online, it seems to work just fine but I feel there are some innecessary lines there:
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// this constant won't change:
const int Up_buttonPin = 2; // the pin that the pushbutton is attached to
const int Down_buttonPin = 3;
volatile int Counter;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int down_buttonState = 0; // current state of the up button
int down_lastButtonState = 0; // previous state of the up button
bool bPress = false;
void setup()
{
Serial.begin(9600);
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
// initialize the lcd
// Print a message to the LCD.
lcd.begin(16, 4);
lcd.setCursor(0,0);
lcd.print("Contador de Pulsos");
lcd.setCursor(2,1);
lcd.print(buttonPushCounter);
}
void loop()
{
checkUp();
checkDown();
if( bPress){
bPress = false;
lcd.setCursor(1,1);
lcd.print(" cm");
lcd.setCursor(1,1);
lcd.print(buttonPushCounter);
}
//This will select the target size based on a physically latching switch
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState) {
// if the state has changed, increment the counter
if (up_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState) {
// if the state has changed, increment the counter
if (down_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
down_lastButtonState = down_buttonState;
}
the void check up and the void check down seem a little useless for what I need but I'm not sure if I should erase them.
Now, as for my current code, I copied some part of the one above and placed them in the button sketch:
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
/*
This example code is in the public domain.
*/
// constants won't change. They're used here to set pin numbers:
const int Up_buttonPin = 2; // the number of the pushbutton pin
const int Down_buttonPin = 3;
volatile int count = 0;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
bool bPress = false;
void setup() {
Serial.begin(9600);
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
// Print a message to the LCD.
lcd.begin(16, 4);
lcd.setCursor(0,0);
lcd.print("Contador de Pulsos");
lcd.setCursor(2,1);
lcd.print(buttonPushCounter);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(Up_buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if ( bPress){
bPress = false;
count++; //add 1 to the count
lcd.setCursor(1,1);
lcd.print(" cm");
lcd.setCursor(1,1);
lcd.print(buttonPushCounter);
}
if (count>=8){
count =0;
lcd.setCursor(1,1);
lcd.print(" cm");
lcd.setCursor(1,1);
lcd.print(buttonPushCounter);
}
}
No errors come up when I verify the code, but when I press the buttons in my protoboard, nothing happens, and I'm not certain of what's causing it