Pages: [1]   Go Down
Author Topic: for loop not working  (Read 284 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guys

I must be doing something stupid cos my for loop doesnt seem to work

any help gratefully received

sorry forgot to say, the problem is that the fadeValue doesnt decrement.

Code:
else if (totaltime >= (1124))  
         {
              do
             {
                 // fade out from max to min in increments of 5 points:
                  Serial.println ("sunset");
                  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
                  {
                    
                       Serial.println(fadeValue);
                        // sets the value (range from 0 to 255):
                       analogWrite(whiteled, fadeValue);
                       analogWrite(blueled, fadeValue);        
                       // wait for 30 milliseconds to see the dimming effect
                       delay(30);                            
                       if (digitalRead(stormswitch) == HIGH)
                         {
                            break;
                          }
                   }
                }while (totaltime <= (1350));
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want people to reproduce the problem, you need to post all of the code. The for loop look fine to me. What are you seeing in the serial monitor?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12524
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be best to post a complete sketch so that we have a context for this code. For example, have you proved that the code is actually executing at all? If your sketch contains a significant amount of unrelated code then it would be best to create a test sketch that only contained the minimal code necessary to demonstrate the problem. Creating this will also help you to understand the scope of the problem and force any false assumptions to the surface - it may even enable you to solve the problem yourself. At the very least, it will make it easier for us to help you.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 32
Posts: 1377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The break will only apply to the for loop, so the for loop will be immediately re-executed if stormswitch is HIGH. Are you sure the switch is usually LOW?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is what i do sometimes...i comment out all the code in the loop itself and just do a serial print to see if it is infact looping correctly...then i start to add 1 line back in at a time...then when it breaks...that is the line u need to look at...i just did this for a project i am working on and have posted here for feedback too.
Logged

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

see full code below, the serial monitor shows the following
 RTC has set the system time
sunset
255
sunset
255
sunset
255
sunset
255
sunset
255
sunset
255
sunset
255
sunset

etc

I would have expected it to show

 RTC has set the system time
sunset
255
250
245
240

etc




Code:
#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#define BETWEEN 2579
#define DURATION 43
#define TIMES 7
#define whiteled 10
#define blueled 11
#define stormswitch 2

unsigned long lastTime = 0;
int waitTime = 0;
int Val = 0;
int burst; //short burst of light
int big;
int srumble;
int brumble;
int mixer;
int diming;
int delaymix;
int dark;
int lightningtimer;
int lightningtime;
byte sunrise = 420;
byte daytime = 450;
byte sunset = 1260;
byte night = 1350;
int totaltime = 0;

void setup() 
{
  pinMode(stormswitch, INPUT);
  digitalWrite(stormswitch, HIGH);
  pinMode(whiteled, OUTPUT);
  pinMode(blueled, OUTPUT);
  Serial.begin(9600);
  //  while (!Serial) ; // wait until Arduino Serial Monitor opens
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void loop()
{
  if (timeStatus() == timeSet)
     {
      if (digitalRead (stormswitch) == LOW)
       {
         storm();
       }
      else
      do
       {
         int totaltime = (hour()*60) + minute();
   
         //  digitalClockDisplay();
         if (totaltime >= (1350))
          {
             Serial.println ("night");
             analogWrite (whiteled, 0);
             analogWrite (blueled, 0);
          }
         else if (totaltime >= (1124)) 
         {
              do
             {
                 // fade out from max to min in increments of 5 points:
                  Serial.println ("sunset");
                  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
                  {
                   
                       Serial.println(fadeValue);
                        // sets the value (range from 0 to 255):
                       analogWrite(whiteled, fadeValue);
                       analogWrite(blueled, fadeValue);         
                       // wait for 30 milliseconds to see the dimming effect
                       delay(30);                           
                       if (digitalRead(stormswitch) == HIGH)
                         {
                            break;
                          }
                   }
                }while (totaltime <= (1350));
            }
         else if (totaltime >= (450))
            {
                Serial.println ("Daytime");
                analogWrite(whiteled, 255);
                analogWrite(blueled, 255);
            }
         
         else if (totaltime >= (420))
            {
                do
                {
                  //30 Min sunrise
                   Serial.println ("sunrise");
                  for(int fadeValue = 0; fadeValue < 191; fadeValue +=1)
                   {
                      analogWrite(whiteled, fadeValue);
                      analogWrite(blueled, fadeValue);
                      if (digitalRead(stormswitch) == HIGH)
                      {
                         break;
                      }
                   }
                }while (totaltime >= (450));
            }
          else
            {
              Serial.println ("night");
              analogWrite (whiteled, 0);
              analogWrite (blueled, 0);
              if (digitalRead(stormswitch) == HIGH)
                {
                 break;
                }
            }   
        } while (digitalRead (stormswitch) == LOW);
    }
   else
    {
        Serial.println("The time has not been set.  Please run the Time");
        Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
        Serial.println();
        delay(4000);
    }
    delay(1000);
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  Serial.print(" ");
  Serial.println(); 
 }

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
void storm ()
{
    digitalWrite(blueled, LOW);
    Serial.println("lightning");
    if (millis() - waitTime > lastTime)  // time for a new flash
    {
      // adjust timing params
      lastTime += waitTime;
      waitTime = (random(BETWEEN)*1.5);

      for (int i=0; i< random(TIMES); i++)
        {
           //Serial.println(millis());
           digitalWrite(whiteled, HIGH);
           delay(100 + random(DURATION));
           digitalWrite(whiteled, LOW);
           delay(10);
        }
    }
}
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
                       if (digitalRead(stormswitch) == HIGH)
                         {
                            break;
                          }
This will break out of the for loop, not the do/while statement. Is that what you want? It looks like you should be comparing the digitalRead value to LOW, not HIGH. How is the switch wired?
Logged

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

d'oh yeah now it cycles correctly, thanks all
Logged

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

however now it appears it wont jump to void storm when the stormswitch is low :-S
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12524
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now it appears it wont jump to void storm ...

Presumably you've addressed the previous problem by changing your code. If you want help with your new code, you need to post it.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: