Stopping a Loop


hi, starting learn more arduino , programming have hit dead end , need little guidance direction go next.

basically want make script measures analogue input pin, when inside specified range yellow led illuminates, when outside range want green led illuminate 8 seconds , switch off. codes (see below) switch green led off single operation , loop , stay on 8 seconds. other issue if analogue input increases acceptable region while in 8 second delay, not instantly interrupt until 8 seconds up.

code: [select]
*/

int sensorpin = a0;    // select input pin potentiometer
int yellowled = 13;
int greenled = 12;
int sensorvaluemax = 600;  // variable store value coming sensor
int sensorvaluemin = 200;

void setup() {
 pinmode(yellowled, output);
 pinmode(greenled, output);
}

void loop() {
 // read sensor:
 int sensorreading = analogread(a0);


if (sensorvaluemax > analogread(sensorpin) && sensorvaluemin < analogread(sensorpin)){
    digitalwrite(yellowled, high);
    digitalwrite(greenled, low);
}
else
{
    digitalwrite(yellowled, low);
    digitalwrite(greenled, high);
    delay(8000);
    digitalwrite(greenled, low);    
}
}


i have tried experiment attachinterrupt not luck.

if give guidance on go/ try next grateful

thanks :)


the other issue if analogue input increases acceptable region while in 8 second delay, not instantly interrupt until 8 seconds up.


welcome wonderful world of delay(), otherwise known sitaroundandtwiddlemythumbs(). take @ blink without delay example writing non-blocking code can emulate "delay".

code: [select]

int sensorpin = a0;    // select input pin potentiometer
...
 int sensorreading = analogread(a0);


why bother giving pin name if you're not going use it?

code: [select]

 int sensorreading = analogread(a0);

if (sensorvaluemax > analogread(sensorpin) && sensorvaluemin < analogread(sensorpin)){


why bother storing results of reading in variable , not using variable?

code: [select]

    digitalwrite(yellowled, low);
    digitalwrite(greenled, high);
    delay(8000);
    digitalwrite(greenled, low);    


this piece of code run long sensor still within acceptable range, meaning after turn green led off, few microseconds of code execute, , turned on again 8 seconds. you're going have define requirements little better. after 8 seconds, led should turn off? until when? if sensor still within acceptable range?

quote
i have tried experiment attachinterrupt not luck.


not surprised, nothing in code screams i need interrupt.


Arduino Forum > Using Arduino > Programming Questions > Stopping a Loop


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62