SimpleTimer

Hi,
I am new to Arduino
I am trying to use the Simpletimer to turn off the button in 5 seconds if it is left on
If I comment out timer.run(); then the est of the program will run but of course not timeout
if I uncomment the line then the program compiles but if I send a ‘d’ it does nothing
What am I missing?

Thank You

/*
Name: PushButtonBot.ino
Created: 8/31/2016 11:17:18 AM
Author: TAadmin
*/
//Include any library needed
#include <SimpleTimer.h>

// Define default variables
char incomingbyte = ‘a’;
int pushButton = 6;
boolean buttonState = false;
SimpleTimer timer;

// Function to turn of button
void buttonTimeout() {
digitalWrite(pushButton, HIGH);
buttonState = false;
}

// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
pinMode(pushButton, OUTPUT);
digitalWrite(pushButton, HIGH);
buttonState = false ;
timer.setInterval(5000, buttonTimeout);
}

// the loop function runs over and over again until power down or reset
void loop() {
// Read any incoming characters from the USB
incomingbyte = Serial.read();

// Read current button state 0 = off 1 = On
Serial.println(buttonState);

if (incomingbyte == ‘u’){
digitalWrite(pushButton, HIGH);
buttonState = false;

}

if (incomingbyte == ‘d’){
digitalWrite(pushButton, LOW);
buttonState = true;

timer.run();
}

delay(10);
}

upDate: The button will engage but does not timeout in 5 seconds or ever

Hi,
timer.run() should be always called. Instead you call it only when "d" is received.

incomingbyte = Serial.read(); You should read only if there is something to read.

https://www.arduino.cc/en/Serial/Read

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

How have you got your button wired, do you have a pull down resistor to pull our input down when your button is open?

Tom.... :slight_smile: