Hi all arduino lovers,good day to you!
The problem I have is pretty simple but the solution seem to be out of my reach still.
I am trying to fix a coin timer which had it's board burnt by replacing it with an arduino Duemilanove.
Wiring:
1 input switch that kinda bounce a bit and that is pushed for like 50ms when a quarter comes in-->Pin2
1 Output Relay (currently just a led) on pin 13
What i'm trying to do:
-When you insert a quarter,you increment the timer by 3 minutes
-As soon as there is time available,the LED output/relay must be activated for that duration
-(here's the tricky part) say you have 2 minutes left,you insert another quarter and the timer must again increment by 3 so we now have 5 minutes left.
It all seem pretty simple I know,1 switch and 1 led but I can't get it to work correctly let's say for a reliable time of 15min.1 quarter works great when the switch bouncing problem ain't in the way but arduino timing ain't good and interupt are kinda weird acting too.
Here's the code I've done so far;
const int buttonPin = 2; // the number of the pushbutton pin
const int relayPin = 13; // the number of the LED pin
// variables will change:
int buttonstate = 0; // variable for reading the pushbutton status
long timefactor = 1800;
int relaystate = 0 ;
long timeleft= 0;
void setup() {
timeleft=0;
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(timeleft);
// Output Relay
if (relaystate==1){
digitalWrite(relayPin, HIGH);
}
if (relaystate==0){
digitalWrite(relayPin, LOW);
}
//Trigger pressé?
if (digitalRead(buttonPin)==HIGH){
do
{
buttonstate=(digitalRead(buttonPin));
}
while (buttonstate==HIGH);
delay(200);
timeleft=timeleft+timefactor;
}
delay(100);
timeleft=timeleft-1;
if (timeleft>1){
relaystate=1;
}
if (timeleft<1){
relaystate=0;
timeleft=0;
}
}
You can tear it apart if you want,I'd just want the simplest working solution.
I'm kinda short on time and coding ain't my best domain so if someone would give it a try I would greatly appreciate it.
thanks and have a nice day ;p