hi, first time using arduino uno , i'm making switch 2 inputs. right have 1 second delay before switch gets triggered, make 30 second delay , when pin state changes before time runs out, cancel pin trigger , loop again.
i have searched forum, guess there's information me handle right now.
i appreciate every push in right direction.
my code far
i have searched forum, guess there's information me handle right now.
i appreciate every push in right direction.
my code far
code: [select]
const int powsw = 13; //computer power switch
const int compon = 4; //computer on
const int powon = 7; //power on
void setup() {
pinmode(powsw, output);
pinmode(compon, input);
pinmode(powon, input);
}
void loop() {
// power off
if ((digitalread(compon) == high) && (digitalread(powon) == low)) {
delay(1000);
triggerpin(powsw);
}
// power on
if ((digitalread(compon) == low) && (digitalread(powon) == high)) {
delay(1000);
triggerpin(powsw);
}
}
void triggerpin(int pin) {
digitalwrite(pin, high);
delay(100);
digitalwrite(pin, low);
delay(100);
}
we use millis() timers things that...
blar blar blink out delay blar blar
heres test code
blar blar blink out delay blar blar
heres test code
code: [select]
const int powsw = 13; //computer power switch
const int compon = 4; //computer on
const int powon = 7; //power on
unsigned long previousmillis1 = 0;
unsigned long previousmillis2 = 0;
unsigned long interval1 = 5000;//1000 per second
unsigned long interval2 = 5000;
void setup() {
pinmode(powsw, output);
pinmode(compon, input);
pinmode(powon, input);
}
void loop() {
unsigned long currentmillis = millis();
// power off
if ((digitalread(compon) == high) && (digitalread(powon) == low)) {
if (currentmillis - previousmillis1 >= interval1) {//wait
triggerpin(powsw);//after wait this
}
} else {
previousmillis1 = currentmillis;//keep reseting timer when not in use
}
// power on
if ((digitalread(compon) == low) && (digitalread(powon) == high)) {
if (currentmillis - previousmillis2 >= interval2) {
triggerpin(powsw);
}
} else {
previousmillis2 = currentmillis;
}
}
void triggerpin(int pin) {
digitalwrite(pin, high);
delay(100);
digitalwrite(pin, low);
delay(100);
}
Arduino Forum > Using Arduino > Programming Questions > Cancel timer on pin state change
arduino
Comments
Post a Comment