Pages: [1] 2   Go Down
Author Topic: Help! Trying to modify Sketch  (Read 932 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:


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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have but am having issues implimenting it...
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think your goal is to let the LEDS blink one by one for 300 msec, right?

something like this?
Code:
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

Rob Tillaart

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

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you right. i wrote that quick example on the move anyway.
Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

actually...NOT !!
Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something wrong in your code but cant see what it is, tbh
Logged

10 LET Loop=Infinite
20 GO TO 10

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

please retry.
Logged

Rob Tillaart

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

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: