Author Topic: Code problem from delay to TimerOne Arduino  (Read 5405 times)

0 Members and 1 Guest are viewing this topic.

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Code problem from delay to TimerOne Arduino
« on: August 15, 2012, 11:59:08 pm »
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: [Select]
buttonPin2State = digitalRead(buttonPin2);

if((buttonPin2State== HIGH) && (digitalRead(ledPin3) == HIGH)) {
for (count=0;count<5;count++ ) {
digitalWrite(pinArray[count], HIGH);
delay(timer*2);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
}
else if ((buttonPin2State == HIGH) && (digitalRead(ledPin5) == HIGH)) {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(500);
}
}
else if((buttonPin2State== HIGH) && (digitalRead(ledPin6) == HIGH)) {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(400);
digitalWrite(pinArray[count], LOW);
delay(200);
}}}



Ive made it easier lets put this into a sort of case:


« Last Edit: August 17, 2012, 02:47:15 am by c3d20 »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: Code problem from delay to TimerOne Arduino
« Reply #1 on: August 16, 2012, 01:28:53 am »
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: [Select]
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: [Select]
void loopLeds()
{
    if (FlashLedPin  != 0) 
    {
       switch (FlashState)   
       {
  case 0:  // dummy state to clear variables
  WaitcountHigh = 0;
                WaitcountLow = 0;
                WaitOver = FALSE;
                // break statement intentionally left out
 
  case 1: // turn led on and wait for number of ticks        
digitalWrite(pinArray[count], 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 ticks
      digitalWrite(pinArray[count], 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 occurrence
                FlashLedPin = 0;
          break
      }
   }   
}
« Last Edit: August 16, 2012, 02:27:33 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #2 on: August 16, 2012, 02:14:58 am »
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 up ledArray2 for example 1 second faster

or  if Button2 is pressed during ((digitalRead(ledPin5) == HIGH) then it speeds up ledArray2 2 second faster

or if Button2 is pressed during ((digitalRead(ledPin6) == HIGH) then it speeds up ledArray2 3 seconds faster

and to better understand:

 ledArray2 is a car on a race track that goes around and around at 60mph
and ledArray1 is 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 or
we 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 seconds :)

I hope this explains it better, it has been fun explaining it like this.
« Last Edit: August 16, 2012, 02:21:54 am by c3d20 »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: Code problem from delay to TimerOne Arduino
« Reply #3 on: August 16, 2012, 02:21:36 am »
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.
« Last Edit: August 16, 2012, 02:33:04 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #4 on: August 16, 2012, 02:43:17 am »
Great thanks Ill get the breadboard out and give it a go. If you know of any other ways let me know. Cheers mate
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #5 on: August 16, 2012, 12:03:32 pm »
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
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: Code problem from delay to TimerOne Arduino
« Reply #6 on: August 16, 2012, 12:14:01 pm »
Can you explain how you have this setup hardware wise?

I'm still not sure exactly what you're trying to do.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Code problem from delay to TimerOne Arduino
« Reply #7 on: August 16, 2012, 12:16:47 pm »
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 :)
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #8 on: August 16, 2012, 12:45:04 pm »
Can you explain how you have this setup hardware wise?

I'm still not sure exactly what you're trying to do.

I have breadboard, arduino Uno, leds, buttons, resistors etc.

Im making a light diplay that sort of replicates a train on a track but with leds.

I will be using a larger led set up and include other functions and copying data onto a    
ATmega8A so pins aren't a problem and I also have learnt multiplexing using shift registers and other techniques.All I need help with is this part though.

So what I have done already is design the code for the train on the track represented by lights

and I have done the code for the speed increase lights

so now what I want to do is add 1 button to each speed increase light 1,2,3 at the moment its programmed to 1st comes on then 2nd, 3rd, so when its pressed on one of the three lights the speed of the train goes faster


So if Button2 is pressed during (((ledPin3) == HIGH) then it speeds up ledArray2 for example 1 second faster

or  if Button2 is pressed during ((ledPin5) == HIGH) then it speeds up ledArray2 2 second faster

or if Button2 is pressed during ((ledPin6) == HIGH) then it speeds up ledArray2 3 seconds faster
 

Offline dda

  • Contributor
  • Posts: 46
  • Country: au
Re: Code problem from delay to TimerOne Arduino
« Reply #9 on: August 16, 2012, 01:08:23 pm »
I'm not sure I would want to travel in a train that was controlled like this.
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #10 on: August 16, 2012, 01:21:51 pm »
I'm not sure I would want to travel in a train that was controlled like this.

Its for model railway project
 

Offline dda

  • Contributor
  • Posts: 46
  • Country: au
Re: Code problem from delay to TimerOne Arduino
« Reply #11 on: August 16, 2012, 01:40:42 pm »
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.
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Code problem from delay to TimerOne Arduino
« Reply #12 on: August 16, 2012, 01:51:47 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf