Go Down

Topic: Please help very very lost.. (Read 977 times) previous topic - next topic

miami_robot

Hi Everyone,

I am trying to write the code where two leds blink and one led fades. It is combining the two Arduino tuts but I want them to run for ten minutes or until power is off.

I just can't get my mind around writing the condition - while, do while, if ...
please help me back on the road. I've had enough issues just getting my Unos to work.

Attached is my code.

Thank you.

Mike

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.

*/
int sionMax = 100;
int sionValue = 0;
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

void setup()  {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  // declare pin 8 is left eye
  pinMode(8, OUTPUT);
  //declare pin 7 is right eye
  pinMode(7, OUTPUT);
}

void loop() {
  while(sionValue < sionMax)
  //loop to blink eyes
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(50);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(5200);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(100);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(3200);
  // end to blink eyes

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 60) {
    fadeAmount = -fadeAmount ;
    sionValue = sionValue + 1;
  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(40);                           
}

pocketscience

For starters you're missing the brackets around your while loop code...

Code: [Select]
while(sionValue < sionMax)
{
  //loop to blink eyes
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(50);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(5200);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(100);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(3200);
  // end to blink eyes
}



G.
Is life really that serious...??!

jwatte

That loop code will never finish, because neither of the variables are modified within the loop.
Also, one of the variables is only modified inside an if() that doesn't always fire, which also means that the while loop will (likely) never finish.

miami_robot


Thank you for your comments. I am a little confused about what is happening with the code.
IT seems to "blink" but never make it to the "fade". Is that because of the delays(xxx); with the 8 and 7 pin?

I've tweaked the code as the only condition is while the power is on it should both blink and fade simultaneously.

Can anyone suggest how I get both functions work together?

Any assistance would be helpful.

new code:
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.

*/
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

void setup()  {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  // declare pin 8 is left eye
  pinMode(8, OUTPUT);
  //declare pin 7 is right eye
  pinMode(7, OUTPUT);
}

void loop() {

    //loop to blink eyes
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(5200);
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(3000);

   analogWrite(9, brightness);   
  // end to blink eyes

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 100) {
    fadeAmount = -fadeAmount ;

  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);                           
}

PaulS

Code: [Select]
//loop to blink eyes
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(5200);
digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(3000);

   analogWrite(9, brightness);   
  // end to blink eyes

There's no loop here. This code will take 8.22 seconds to complete.

Code: [Select]
  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 100) {
    fadeAmount = -fadeAmount ;

The range of values for fading is 0 to 255, not 0 to 100. You will get to less than half the brightness before you start dimming again. It is unlikely that you will discern brightness differences of 1 every 8.25 seconds.

Go Up