button - Start/Stop a function using pushbutton -
i trying display readings of ldr on 0.96" adafruit oled. have succeeded obtaining results. want start display of reading when push button , stop once push again. should go on loop. tried draft code that:
#include <spi.h> #include <wire.h> #include <adafruit_gfx.h> #include <adafruit_ssd1306.h> int sensorpin = a0; // select input pin ldr int sensorvalue = 0; boolean state = false; int buttonpin; #define oled_reset 4 // not used / nicht genutzt bei diesem display adafruit_ssd1306 display(oled_reset); char inchar; string string; void setup() { pinmode(13, output); buttonpin = 2; //whatever pin button plugged pinmode(buttonpin, input_pullup); // initialize i2c addr 0x3c / mit i2c-adresse 0x3c initialisieren display.begin(ssd1306_switchcapvcc, 0x3c); serial.begin(9600); display.display(); delay(2000); display.cleardisplay(); display.settextcolor(inverse); } void loop() { while (state == false) { if (digitalread(buttonpin) == high) { display.cleardisplay(); sensorvalue = analogread(sensorpin); serial.println(sensorvalue); display.setcursor(30,0); display.settextsize(1); display.print("ldr reading:"); display.setcursor(30,10); display.settextsize(2); display.print(sensorvalue); delay(500); display.display(); state = false; } } } but screen starts displaying results when keep push button pressed continuously , when release button , program halts last reading displayed on screen.
i need results follows:
1st press: starts displaying readings
2nd press: screen should blank. (display.cleardisplay() job)
i unfamiliar usage of switch case in arduino.
keep boolean track toggle state , boolean keep state of button previous loop. if button goes low high between loop iterations update toggle boolean,
toggle = !togggle base condition display state on toggle instead of directly on button.
Comments
Post a Comment