Go Down

Topic: Blinking LED help! (Read 596 times) previous topic - next topic

Hang

Hi everyone,

I'm new to the Arduino board. I am currently experimenting with it.  I am having trouble with a small program that I wrote, hope you guys can help. The purpose of this program is to make 4 LEDs blinking fast in a circle pattern for 10 rounds. Then slow it  down. Repeat. However, when it slows down it doesn't go back to blinking fast again. It just keep slowing down until it comes to a complete stop.

Code: [Select]
int pin12 = 12;
int pin11 = 11;
int pin10 = 10;
int pin9 = 9;
int timer = 100;
int count = 0;
int maxcount = 10;
int countdown = 0;

void setup(){

 pinMode(pin12, OUTPUT);
 pinMode(pin11, OUTPUT);
 pinMode(pin10, OUTPUT);
 pinMode(pin9, OUTPUT);

}

void loop() {

  if (count < maxcount)
  {
    count = count + 1;          
  }
  else
  {
   
    count = 0;
    if (countdown = 0) {
      countdown = 1;
      }
    else {countdown = 0;}
  }
   
  if (countdown = 1)
  {timer = timer + 25;

   }
  else {timer = timer - 100;}

  digitalWrite(pin9, HIGH);
  delay(timer);
  digitalWrite(pin9, LOW);
  delay(timer);

  digitalWrite(pin10, HIGH);
  delay(timer);
  digitalWrite(pin10, LOW);
  delay(timer);

  digitalWrite(pin12, HIGH);
  delay(timer);
  digitalWrite(pin12, LOW);
  delay(timer);

  digitalWrite(pin11, HIGH);
  delay(timer);
  digitalWrite(pin11, LOW);
  delay(timer);
 }

jims

if ( countdown = 1) ... you mean to have a "==" there.
That will get you to your next bug.  (timer will go down by 100 each time into negative numbers, causing huge delays or near zero delays depending how delay() is written.)

You may wish to put in some serial printing as debugging info while you work out the logic...
Code: [Select]
Serial.print("count=");
Serial.print(count);
Serial.print(" countdown=")
Serial.print(countdown);
Serial.print(" timer=");
Serial.print(timer);
Serial.println();

.. click the little rectangle with a lollypop sticking out of the top in the toolbar to get the serial monitor running after you download your program.

Hang

Thanks Jim,

I got the program to work now. As for the serial.print command that you mention I was wondering how do you use that in the program. I included the snipet of codes you wrote into my program and I open up the serial monitor, but I don't see anything displaying on the screen. I was wondering if you could explain how it works. Thanks for the help again.

libhart

In your setup() function, add this

Serial.begin(19200);

The 19200 is just a baud rate, which can be set in the dropdown box that appears when you click the serial monitor (lolipop) button.  That should make the commands Jim gave send output.

Hang

Thanks for the help libhart. Got it working.

Go Up