Code problem from delay to TimerOne Arduino - Page 1
Forum section: electronics / beginners

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 11:59:08 pm
content: The script has been compiled using Arduino Uno.I am stuck with how to rewrite this snippet of code into my Timer1 script. I got the basic idea of what I wanted to do. It played around with the delays to speed up a 5 led display blink array on the press of a button. How could I change it to work in the revised Timer1 script:Code:buttonPin2State = digitalRead(buttonPin2);if((buttonPin2State== HIGH) && (digitalRead(ledPin3) == HIGH)) {for (count=0;count<5;count++ ) {digitalWrite(pinArray, HIGH);delay(timer*2);digitalWrite(pinArray, LOW);delay(timer);}}else if ((buttonPin2State == HIGH) && (digitalRead(ledPin5) == HIGH)) {for (count=0;count<5;count++) {digitalWrite(pinArray, HIGH);delay(timer);digitalWrite(pinArray, LOW);delay(500);}}else if((buttonPin2State== HIGH) && (digitalRead(ledPin6) == HIGH)) {for (count=0;count<5;count++) {digitalWrite(pinArray, HIGH);delay(400);digitalWrite(pinArray, LOW);delay(200);}}}Ive made it easier lets put this into a sort of case:

author: Psi
author_rank: Super Contributor
timestamp: August 16, 2012, 01:28:53 am
content: If i understand your first code block correctly (and i'm not sure i do, you seem to have exactly the same for loop for each 'elseif')Then maybe something like this..It updates the buttonpin2state in the main loop and flags when a button flash cycle needs to occur.The button flash then happens in a case statement inside the timer interrupt.(Only new code shown)-- Main LOOP --Code:void loop(){buttonPin2State = digitalRead(buttonPin2);if ( (FlashLedPin == 0) && (buttonPin2State == HIGH) ){if ((digitalRead(ledPin3) == HIGH){FlashLedPin = 3;FlashState = 0;}if ((digitalRead(ledPin5) == HIGH){FlashLedPin = 5;FlashState = 0;}if ((digitalRead(ledPin6) == HIGH){FlashLedPin = 6;FlashState = 0;}}}-- Timer Interrupt -- (###) is where you set your delays in terms of timer ticks (you will want to speed up the timer)Code:void loopLeds(){if (FlashLedPin != 0){switch (FlashState){case 0: // dummy state to clear variablesWaitcountHigh = 0;WaitcountLow = 0;WaitOver = FALSE;// break statement intentionally left outcase 1: // turn led on and wait for number of ticksdigitalWrite(pinArray, HIGH);WaitcountHigh = WaitcountHigh + 1;switch (FlashLedPin){case 3: if (WaitcountHigh >= ###) FlashState = 2; break;case 5: if (WaitcountHigh >= ###) FlashState = 2; break;case 6: if (WaitcountHigh >= ###) FlashState = 2; break;}break;case 2: // turn led off and wait for number of ticksdigitalWrite(pinArray, LOW);WaitcountLow = WaitcountLow + 1;switch (FlashLedPin){case 3: if (WaitcountLow >= ###) WaitOver = TRUE; break;case 5: if (WaitcountLow >= ###) WaitOver = TRUE; break;case 6: if (WaitcountLow >= ###) WaitOver = TRUE; break;}if (WaitOver == TRUE){FlashCount = FlashCount + 1;if (FlashCount >= 5) FlashState=3 else FlashState=0;}break;case 3: // reset flag for next occurrenceFlashLedPin = 0;break}}}

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 02:14:58 am
content: Thanks for your reply PSI. It might be okay it looks like it might be workable. Will it do this\/?So if Button2 is pressed during ((digitalRead(ledPin3) == HIGH) then it speeds upledArray2for example 1 second fasteror if Button2 is pressed during ((digitalRead(ledPin5) == HIGH) then it speeds upledArray22 second fasteror if Button2 is pressed during ((digitalRead(ledPin6) == HIGH) then it speeds upledArray23 seconds fasterand to better understand:ledArray2is a car on a race track that goes around and around at 60mphandledArray1is a visual of say a speed increaser, so if ((digitalRead(ledPin3) comes on and we press the button it makes the car go a bit faster at 70MPH for 1 sec orwe decide to wait until the next led to come on((digitalRead(ledPin5) so we press and it makes the car go even faster 70MPH for 2 seconds or we decide to wait until the next led to come on ((digitalRead(ledPin6) so we press and it makes the car go at light speed 70 MPH for 3 secondsI hope this explains it better, it has been fun explaining it like this.

author: Psi
author_rank: Super Contributor
timestamp: August 16, 2012, 02:21:36 am
content: dunno, i wasn't looking at your existing interrupt code, I was only trying to convert your first code block with delays into an interrupt based code block as that is where a lot of people have trouble.You just need to set the timer to say 100ms then you can set those ### bits.A ### of 10 will make a 10 * 100ms = 1 second delay.So the values in those six ### blocks will vary the on and off time for the 3 possible states.

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 02:43:17 am
content: Great thanks Ill get the breadboard out and give it a go. If you know of any other ways let me know. Cheers mate

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 12:03:32 pm
content: Im having big trouble rewriting the code in to my Timer1 script\/: I dont even know where to start. Will it even work to do what I want this new code to do?. Can someone help me include the new code type into my script? Im a novice to this and this new bit of code is too complex for me. Thanks

author: Psi
author_rank: Super Contributor
timestamp: August 16, 2012, 12:14:01 pm
content: Can you explain how you have this setup hardware wise?I'm still not sure exactly what you're trying to do.

author: Rerouter
author_rank: Super Contributor
timestamp: August 16, 2012, 12:16:47 pm
content: and as a freindly addition for your code, comment it, anything you type on a line following "//" is a comment and will not be parsed,just as your past bits of code would have been much easier to interprit if you explained it out with them

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 12:45:04 pm
content: Quote from: c3d20 on August 16, 2012, 12:39:36 pm

author: dda
author_rank: Contributor
timestamp: August 16, 2012, 01:08:23 pm
content: I'm not sure I would want to travel in a train that was controlled like this.

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 01:21:51 pm
content: Quote from: dda on August 16, 2012, 01:08:23 pmIts for model railway project

author: dda
author_rank: Contributor
timestamp: August 16, 2012, 01:40:42 pm
content: So, the speed controller cycles through SLOW - MEDIUM - FAST options and you just have to hit a button at the right point? Thats nuts.Why not two buttons for Faster / Slower. And just light the SLOW / MEDIUM / FAST LED in response.

author: c3d20
author_rank: Contributor
timestamp: August 16, 2012, 01:51:47 pm
content: No I can only use one button for it and it must be 3 leds, 1st = faster for 1 second, 2nd = fasterx2 for one second, 3rd fasterx3 for 1 second (for example). Very easy if you know how. As I say I ve managed to compile the first code I just need to do this now.