Offline
Newbie
Karma: 0
Posts: 25
|
 |
« on: May 04, 2012, 09:49:15 pm » |
I'd like to do this skectch w/out delays. But don't know what to do...thanks for any help!
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); }
}
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: May 05, 2012, 11:02:21 am » |
Have a look at the blink with out delay tutorial - http://arduino.cc/en/Tutorial/BlinkWithoutDelay -
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #2 on: May 05, 2012, 01:12:28 pm » |
I have but am having issues implimenting it...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #3 on: May 05, 2012, 03:37:26 pm » |
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 !! 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);
}
}
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #4 on: May 06, 2012, 03:17:19 am » |
Think your goal is to let the LEDS blink one by one for 300 msec, right? something like this? 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 }
|
|
|
|
« Last Edit: May 06, 2012, 04:02:13 am by robtillaart »
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #5 on: May 06, 2012, 03:23:38 am » |
you right. i wrote that quick example on the move anyway.
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #6 on: May 06, 2012, 03:26:22 am » |
actually...NOT !!
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #7 on: May 06, 2012, 03:31:16 am » |
Something wrong in your code but cant see what it is, tbh
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #8 on: May 06, 2012, 04:02:53 am » |
I can, I missed the () after millis(), I fixed it in the code above.
please retry.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #9 on: May 06, 2012, 04:04:36 am » |
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. 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); } }
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #10 on: May 06, 2012, 04:09:06 am » |
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.
|
|
|
|
« Last Edit: May 06, 2012, 04:34:13 am by iyahdub »
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #11 on: May 07, 2012, 06:07:01 pm » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #12 on: May 07, 2012, 06:09:03 pm » |
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.
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 302
|
 |
« Reply #13 on: May 07, 2012, 06:13:49 pm » |
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 !!
|
|
|
|
|
Logged
|
10 LET Loop=Infinite 20 GO TO 10
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #14 on: May 07, 2012, 06:44:57 pm » |
I figured out my stupidity....the code was not "looping" because i never told it to...LOL...its somuch clearer now..
Thanks Everybody
|
|
|
|
« Last Edit: May 07, 2012, 07:04:31 pm by RazorFly »
|
Logged
|
|
|
|
|
|