Go Down

Topic: Trying to set time limit (Read 1 time) previous topic - next topic

skootergrrl

I'm trying to set a time limit on how long an LED blinks.  Ultimately I'll use it for something more complicated, but being a N00b I thought I'd tackle it one piece at a time.

Code: [Select]

int ledPin = 13;                // LED connected to digital pin 13
int Cutout                      // Cutout time

void setup()                    // run once, when the sketch starts
{
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
 {
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for a second

Cutout (1000 * 60 * 10)  // cut out in 10 minutes - this is the number of milliseconds to cutout - 1000 is ms per second, 60 is mins per second, 10 is number of minutes
unsigned long startTime = millis();

  if( millis() - startTime > Cutout)  // check if CUTOUT ms has elapsed since motor started
       break; {
 digitalWrite(ledPin, LOW);  // LED is off
               }

 }


I keep getting and error when I compile:
error: stray '\' in program In function 'void loop()':

Anachrocomputer

Code: [Select]
Cutout (1000 * 60 * 10)  // cut out in 10 minutes - this is the number of milliseconds to cutout - 1000 is ms per second, 60 is mins per second, 10 is number of minutes

Code: [Select]
 if( millis() - startTime > Cutout)  // check if CUTOUT ms has elapsed since motor started


Both of these lines contain a non-ASCII character that looks like a dash when cut/pasted but shows up as a black rectangle in the browser.  The first instance is in a comment, but the second is in an 'if' statement.  I suspect this is the source of your problem!

As for this code:
Code: [Select]
if( millis() - startTime > Cutout)  // check if CUTOUT ms has elapsed since motor started
       break; {
 digitalWrite(ledPin, LOW);  // LED is off
               }


There's a break keyword with no enclosing for, while, do or switch statement.  Are you sure that's what you wanted here?  Remember that the loop function executes repeatedly, for ever.  You can't break out of that infinite loop, but you can make the execution stop by writing your own infinite loop inside the loop function.

AlphaBeta

I started tweaking your code and as the clock here shows 0039 and I've got nothing better to do with my time and insomnia I figured I might as well sit until it compiles. It is untested, but should set ledPin HIGH, then LOW after ten seconds.

Quote


#define ledPin 13
int Cutout = (1000 * 60 * 10);
unsigned long startTime = 0;

void setup(){
 pinMode(ledPin,OUTPUT);
 digitalWrite(ledPin,HIGH);
 startTime=millis();
}

void loop(){
 if ( millis()-startTime > Cutout ){
   digitalWrite(ledPin,LOW);
   startTime=millis();
 }
}


skootergrrl

Thanks for the help.

I changed both of those "dashes" to dashes.  That's the right thing to do, yes?

It still won't compile.  Same error.

Ultimately I want it to "Look for This Situation.  If that situation happens for 10 minutes, Do This Thing.  If the situation changes before 10 minutes, keep looking."

Does that make sense?

AlphaBeta

Quote
Look for This Situation.  If that situation happens for 10 minutes, Do This Thing.  If the situation changes before 10 minutes, keep looking.


That makes sence

Code: [Select]

//your pseudocode into realish code
void loop(){
 if ( situation ){
   conditionMetTime = millis();
   while( situation ){
     if ( !situation ){ break; }
     else if (conditionMetTime + tenMinutes < millis() ){
       doThisThing();
     }
   }
 }
}

skootergrrl

Thank you so much.  Today is Arduino Day 1.  I'll get the hang of it eventually.

AlphaBeta

Ofcourse you will!


Welcome to the team ;)

Go Up