Pages: [1]   Go Down
Author Topic: help with relay duration  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
:D
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, im having trouble writing code for a relay that i want to turn on if a value is received from analog pin 0. The problem is that i want the if statement to stop looping after one run. Heres what i have so far:
Quote
 
 const int rPin =  8;      // the number of the Relay pin
 void setup() {
     pinMode(rPin, OUTPUT);      
 }
 
 void loop() {
   int val = analogRead(0);
     val = map(val, 0, 1023, 0, 10);
if (val == 9) {    
    // turn LED on:    
    digitalWrite(rPin, HIGH);//Turn on the relay
    delay(850);//wait for the drive to open all the way
    digitalWrite(rPin, LOW);//Turn off the relay
  }
   delay(10);
 }

Logged

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

If I understand you correctly, something like this might be your best bet:

Code:
const int rPin =  8;      // the number of the Relay pin
bool stop = false;
 void setup() {
     pinMode(rPin, OUTPUT);      
 }
 
 void loop() {
   if (stop)
      return;
   int val = analogRead(0);
     val = map(val, 0, 1023, 0, 10);
if (val == 9) {    
    // turn LED on:    
    digitalWrite(rPin, HIGH);//Turn on the relay
    delay(850);//wait for the drive to open all the way
    digitalWrite(rPin, LOW);//Turn off the relay
    stop = true;
  }
   delay(10);
 }

You might want to put in a delay, because this code will loop very fast when stop is set. If you want to stop the program completely, of course you could do that also, but I'm guessing you don't want that.
Logged

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

I actually would like to stop the program completely. But ill give your method a try. thanks for the quick reply.
Logged

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

According to this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294267256/14 ...there's actually no way to stop to microprocessor completely (I learning something today...).

I suppose you could cause a segfault, but that's hardly neat. The infinite loop might be your best bet.
Logged

Pages: [1]   Go Up
Jump to: