I am messing with my little OLED display and would like to make a basic digital number clicker that would just simply add 1 to the previous number every time a normally open switch is closed, and display that number. I have finagled with the code several times but have not been able to get it to work. All it does is display a 0 and the button seems to have no effect. I have tried pull-down resistors but they also have not worked. As of right now, my OLED is connected to the Arduino with the OLED's SDA to A4 and SCL to A5. The switch is connected to 3.3v and the other end waiting to be closed is on A0. I know the OLED is working because I have just used it for another project and I also know that the switch works because I tested it.
I tried both @cherk 's method and @JCA34F 's methods and they did not seem to work. Thanks for the help! this is what it looks like now:
#include <Adafruit_SSD1306.h> #include <splash.h>
Adafruit_SSD1306 display (4);
int CLKR = A0; //button on analog input pin 0
int NUM = 0; //begining number
void setup(){
pinMode(CLKR, INPUT_PULLUP); //making the button on A0 an input
display.begin (0x3C); //telling the arduino of the adress of the oled
display.clearDisplay(); //clear
display.display(); //"initialize"
Serial.begin(9600); //start serial monitor
}
void loop (){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(20,12);
display.println(NUM); //print the value of NUM
display.display(); //display it
int BUTTON = digitalRead(CLKR); //whatever the board reads from A0 is called BUTTON
if (INPUT_PULLUP == LOW){
NUM = NUM ++; // if the board reads that A0 is LOW, change the value of NUM to NUM + 1
Hey i got it to work by changing "NUM = NUM ++" to just "NUM ++". Thanks for the help! i did still utilize both of your tips. the only thing it does now though is sometimes it wont recognize the click. Maybe it isn't low for long enough? And also I would like to make it be able to only click once because right now, if you hold down the button, it just keeps counting.