Go Down

Topic: Help! Trying to modify Sketch (Read 1 time) previous topic - next topic

RazorFly

I'd like to do this skectch w/out delays. But don't know what to do...thanks for any help!

Code: [Select]



int timer = 300;           // The higher the number, the slower the timing.
int ledPins[] = {5, 9, 13, 22};       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

void setup() {
   int thisPin;
   // the array elements are numbered from 0 to (pinCount - 1).
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);     
   }
}

void loop() {
   // loop from the lowest pin to the highest:
   for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
     digitalWrite(ledPins[thisPin], LOW);   
    delay(timer);                 
     // turn the pin off:
     digitalWrite(ledPins[thisPin], HIGH);   

  }

   }

robtillaart


Have a look at the blink with out delay tutorial - http://arduino.cc/en/Tutorial/BlinkWithoutDelay -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

RazorFly

I have but am having issues implimenting it...

iyahdub

Is it something like this ?!? ( Cant check it at the moment, but somewhere around these lines)
But note the fact you should try to understand it properly instead of just having someone to do it. RESEARCH, READ THE TUtORIALS, and always be explicit about things, specially your circuit with schematics or at least a good detailed explanation, otherwise we cant know and guess what is you want !!
Code: [Select]

int ledPins[] = {5, 9, 13, 22};       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;       

void setup() {
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);     
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  for (int thisPin = 0; thisPin < pinCount; thisPin++)

    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPins[thisPin], ledState);   

    }

}

10 LET Loop=Infinite
20 GO TO 10

robtillaart

#4
May 06, 2012, 10:17 am Last Edit: May 06, 2012, 11:02 am by robtillaart Reason: 1
Think your goal is to let the LEDS blink one by one for 300 msec, right?

something like this?
Code: [Select]

int timer = 300;        
int ledPins[] = {5, 9, 13, 22};
int pinCount = 4;
int currentPin = 0;
unsigned long lastLedTime = 0;

void setup()
{
 for (int thisPin = 0; thisPin < pinCount; thisPin++)  
 {
   pinMode(ledPins[thisPin], OUTPUT);
   digitalWrite(ledPins[thisPin], LOW);
 }
}

void loop()
{
 if (millis() - timer > lastLedTime)
 {
   lastLedTime += timer;
   digitalWrite(ledPins[currentPin], LOW);
   currentPin = (currentPin +1 ) % pinCount;
   digitalWrite(ledPins[currentPin], HIGH);
 }
 
 // other code can be here
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

iyahdub

you right. i wrote that quick example on the move anyway.
10 LET Loop=Infinite
20 GO TO 10

iyahdub

10 LET Loop=Infinite
20 GO TO 10

iyahdub

Something wrong in your code but cant see what it is, tbh
10 LET Loop=Infinite
20 GO TO 10

robtillaart


I can, I missed the () after millis(), I fixed it in the code above.

please retry.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

iyahdub

Corrected, though im not sure this is what you wanted. Still, its an example of how to implement it. Hope it helps to get the logic of it, so u can implement it to suit you.
Code: [Select]
int ledPins[] = {
 2, 5, 9, 13 };       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;      

void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 // make the pushbutton's pin an input:
}


void loop() {
 // loop from the lowest pin to the highest:
 // check to see if it's time to blink the LED; that is, if the
 // difference between the current time and last time you blinked
 // the LED is bigger than the interval at which you want to
 // blink the LED.
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
   // save the last time you blinked the LED
   previousMillis = currentMillis;  
   // if the LED is off turn it on and vice-versa:
   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;
 }
 int thisPin;
 // the array elements are numbered from 0 to (pinCount - 1).
 // use a for loop to initialize each pin as an output:
 for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
   pinMode(ledPins[thisPin], OUTPUT);  
   digitalWrite(ledPins[thisPin], ledState);
   Serial.println(ledState);  
 }
}




10 LET Loop=Infinite
20 GO TO 10

iyahdub

#10
May 06, 2012, 11:09 am Last Edit: May 06, 2012, 11:34 am by iyahdub Reason: 1


I can, I missed the () after millis(), I fixed it in the code above.

please retry.

Yeah, thats it. Tired, as i need some rest .
Plus the fact he probably wanted as u did(one at a time)
actually he ended up with two examples to show you can implement it in different ways to suit your style.
as regarding the 300ms he could have changed it at the interval long integer.
10 LET Loop=Infinite
20 GO TO 10

RazorFly

I appreciate the help...however both codes end up with less than desired results...which is kinda why i am ripping my hair out.

One code blinks all 4 at once the other bdoesnt blink...only one light is on.


I kinda hate arduino now...

iyahdub

I tried both and they both work. Mine blinks them all at a time and his does a kind MICHAEL KNIGHT KIT's car effect.
10 LET Loop=Infinite
20 GO TO 10

iyahdub

Arduino is not a toy u can just play and do things without some effort put into it. You cant follow the maxim"If everything else fails, read the instructions" or expect to just copy every thing you think cool, without having some knowledge to know what and when to change this or that !!
Do some reading, start from the beggining if u have no idea of programming and electronics, and climb the stairs...STEP BY STEP !!
robtillaart's code for example is a good example of a nice, tidy code and you can see he is a good programmer.
Dont stress of get frustrated. NO PAIN, NO GAIN !! He didnt learn all that in one day...Noone does !!
10 LET Loop=Infinite
20 GO TO 10

RazorFly

#14
May 08, 2012, 01:44 am Last Edit: May 08, 2012, 02:04 am by RazorFly Reason: 1
I figured out my stupidity....the code was not "looping" because i never told it to...LOL...its somuch clearer now..

Thanks Everybody

Go Up