Optimizing buttonPress code
hello,
i'm quite fresh arduino user , have 1 problem / question.
below code used change 2 leds states (on / off) , change time interval potentiometer , "turn program on/off" pushbutton. leds blinking millis(); function.
photo of prototype:

i'd see opinion code, approach or maybe better use library?
is there better way control state of pushbutton?
is there better solution make program / loop run or stop button?
right when press button program stops, can stop e.g. on 560 ms , when push again run 560 ms , after 440 ms change state of diodes. want run 0 ms after button pressed second time. possible?
ps: sorry english, hope it's ok
i'm quite fresh arduino user , have 1 problem / question.
below code used change 2 leds states (on / off) , change time interval potentiometer , "turn program on/off" pushbutton. leds blinking millis(); function.
code: [select]
const int greenpin = 8; // pin 8 greedpin const
int greenstate = low; // green led low (0)
const int redpin = 4; // pin 4 redpin const
int redstate = low; // red led low (0)
const int btn = 11; // pin 11 btn const
int btncounter = 0; // counter of button pressed states
int btnstate = 0; // button state
int lastbtnstate = 0; // last button state
unsigned long greenprevioustime = 0; // last time green led
unsigned long redprevioustime = 0; // last time red led
void setup() {
serial.begin(9600); //serial debug
pinmode(greenpin, output);
pinmode(redpin, output);
pinmode(btn, input);
}
void loop() {
int btnstate = digitalread(btn); // reads current button state 0 or 1 , saves variable
if (btnstate != lastbtnstate) {
if(btncounter==2){ // if counter = 2 reset 0
btncounter=0;
serial.println("program state: running --> press button stop...");
}
if (btnstate == low) { // if btn pushed down (state low -> 0)
btncounter++; // add 1 counter
}
if(btncounter==1 && btnstate==high){ // without btnstate @ high serial
greenstate = 0; // message printing 2 times
redstate = 0; // turn off leds
digitalwrite(greenpin, greenstate);
digitalwrite(redpin, redstate);
serial.println("program state: stop --> press button start...");
} // delay avoiding errors ??? how avoid ???
delay(50);
}
lastbtnstate = btnstate; // changes last btn state variable current state
if(btncounter==0){ // if counter 0 main code part runs, if true
int timeinterval = analogread(a5); // timeinterval potentiometer (~ 0 -> 1023)
/*
serial.print("time interval: "); // uncomment see current interval in serial monitor
serial.println(timeinterval);
*/
unsigned long greencurrenttime = millis(); // add millis(); red , green led time variables
unsigned long redcurrenttime = millis();
// green led code start **********************
if (greencurrenttime - greenprevioustime >= timeinterval) { // if passed time higher interval
greenprevioustime = greencurrenttime; // previous time current time variable
if (greenstate == low) { // simple dioda state change
greenstate = high;
}else {
greenstate = low;
}
digitalwrite(greenpin, greenstate); // on - off led
}
// red led code start **********************
if (redcurrenttime - redprevioustime >= timeinterval) {
redprevioustime = redcurrenttime;
if (redstate == low) {
redstate = high;
}else {
redstate = low;
}
digitalwrite(redpin, redstate);
}
}
}
photo of prototype:

i'd see opinion code, approach or maybe better use library?
is there better way control state of pushbutton?
is there better solution make program / loop run or stop button?
right when press button program stops, can stop e.g. on 560 ms , when push again run 560 ms , after 440 ms change state of diodes. want run 0 ms after button pressed second time. possible?
ps: sorry english, hope it's ok

code: [select]
digitalwrite(redpin, greenstate);
this not right. perhaps mean redstate?
if want reset time, need set previoustime variables time millis() whenever time needs reset (ie, when decide restart timer.
the approach not efficient should work if not doing else. if part of larger program simplify using structures , arrays manage data better.
Arduino Forum > Using Arduino > LEDs and Multiplexing > Optimizing buttonPress code
arduino
Comments
Post a Comment