Pages: [1]   Go Down
Author Topic: Please help very very lost..  (Read 771 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);                           
}
Logged

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Is life really that serious...??!

Offline Offline
God Member
*****
Karma: 3
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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);                           
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//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:
  // 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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After a lot of reading and thinking I have gotten closer but I am still lost.

I've created a loop of 8,000 while I am going to assume is 8 seconds. Every eight second I want the two leds to blink.
while it is waiting for eight seconds I would like the other led to fade. It seems logical but perhaps I have the fade counting off.

Any help would be appreciated:

/*
  Sion Blink
  Turns on two LEDs to blink every 8 seconds until no power.
  Also fades one LED representing a heart.
 
 */
  int count = 0;
  int brightness = 0;    // The brightness of the LED.
  int fadeAmount = 1;    // how many points to fade the LED. 

void setup()

{
  // initialize the digital pin as an output.
  // Pin 9 is the left eye:
  pinMode(9, OUTPUT);
  // Pin 8 is the right eye:
  pinMode(8, OUTPUT);
  // Pin 7 is the heart:
  pinMode(7, OUTPUT); 
}


void loop()
{
  if (count <= 8,000)
    {
      // set the brightness of pin 7:
      analogWrite(7, brightness);   
      // 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(40);                 
            count++; 
          }
    else 
        { 
          digitalWrite(9,HIGH);
          digitalWrite(8,HIGH);
          delay(500);
          digitalWrite(9,LOW);
          digitalWrite(8,LOW);
          count == 0;
        }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've created a loop of 8,000 while I am going to assume is 8 seconds. Every eight second I want the two leds to blink.
Lousy assumption. There is a function, millis(), that will return the number of milliseconds the Arduino has been running. Record when the LEDs last were flashed. When the difference between "now" and "then" is greater to or equal 8000, flash the LEDs again.

Code:
        if (brightness == 0 || brightness == 100)
The maximum brightness is STILL not 100. It is 255.

Use the Tools + Auto Format option to properly indent your code. Python performs actions based on the level of indenting. C++ does not. Indenting is for humans. The reason for using proper indenting is so that YOU can follow the code.

You will be able to see that your else clause goes with the wrong if statement.

You have two things that you want the Arduino to do. Separate the two actions.

You want one LED to fade from off to on, and back again. There are a certain number of steps required to make this happen. Determine what that number of steps is. It is NOT 200.

Given the number of steps, and the total time (if that is relevant) you can determine the time that the LED should remain at each level.

Keep in mind that the human eye can not perceive the difference in brightness of an LED when analogWrite(pin, 137) and analogWrite(pin, 138) are executed. So, you may want to make the increment a bit larger. Steps of 5 are generally discernible. This will, of course, affect the number of steps, and the time that each steps is to remain in effect.

Once you know how long the step is to last, follow the blink without delay example to fade without delay IGNORING THE NEED TO BLINK THE OTHER LED(s).

When you get that working, it is easy to add the code to make the other action (blinking the other LED(s) every 8 seconds). happen without affecting the fading action.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul I will tackle it again tonight. I've got 100 in my code because 255 is extremely bright when the LEDs fires.
Epileptic seizure comes to mind.

Best,

Mike
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you again Paul. I took "time" out of the equations and focused on the steps like you suggested.
I believe this is solved except for some tweaking for eye blinking. I kept the fade at 255 as this LED is actually under
frosted glass so it will need the strength to fade. The eyes I dropped down to 30 as they are 120 degree blue and burn into your retinas if you keep looking at them.

Best,

Mike

Oh yeah the final code:

/*
 Fading heart and eyes
 Exploring Analogue fade example.
 by Mike R.
 
 */
int Eyepin2 = 9; //LED connected to analog pin 9
int Eyepin = 8;  //LED connected to analog pin 8
int ledPin = 7;    // LED connected to analog pin 7
int fadeset = 0;
int Eye0 = 0;
int Eye150 = 30;   // Can be varied for brightness or change to digital input and use HIGH/LOW
void setup()  {
  // nothing happens in setup
}

void loop()  {
  while (fadeset <= 204) {
    // fade in from min to max in increments of 5 points:
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin, fadeValue);         
      // wait for 30 milliseconds to see the dimming effect   
      delay(50);
      fadeset++;     
    }

    // fade out from max to min in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin, fadeValue);         
      // wait for 30 milliseconds to see the dimming effect   
      delay(50);
      fadeset++;     
    }
  }
  analogWrite(Eyepin,Eye150);
  analogWrite(Eyepin2,Eye150);
  delay(200);
  analogWrite(Eyepin,Eye0);
  analogWrite(Eyepin2,Eye0);
  fadeset = 0;
}





Logged

Pages: [1]   Go Up
Jump to: