Go Down

Topic: Beginner Question: Blinking LED when sensor value = xyz / Loop? (Read 807 times) previous topic - next topic

sublevel

Hello : )

i am new to arduino and programming and i have a general question on the following:
I try to read a sensor input and want the arduino to give feedback with a blinking led.

My code looks like that:

Code: [Select]

int SensorA = 0;
int LedA = 2;
int sensor_val;


void setup() {
Serial.begin(9600);

}
void loop() {
sensor_val = analogRead(SensorA);
Serial.print("Value from sensor");
Serial.println( sensor_val );
delay(4000);

if (sensor_val > 850)
   {
Serial.print("not enough");

// should blink slowly
digitalWrite(LedA, HIGH); 
delay(1000);               
digitalWrite(LedA, LOW);   
delay(1000);   

}   
else if ((sensor_val < 850) && (sensor_val > 500))
{
     
Serial.print("good");
// should be on constantly
digitalWrite(LedA, HIGH); 
delay(5000);               


}
else if (sensor_val < 500)
{
 
Serial.print("too high");
// should blink fast
digitalWrite(LedA, HIGH); 
delay(100);               
digitalWrite(LedA, LOW);   
delay(100);   
}


}


What i do not understand: I want the arduino to make a led blink slowly when the value rises above 850, fast when it drops below 500 and
constantly on when the value is in between. As far as i understand i need some kind of loop like »do this while value < than 500« e.g.
If this is correct: can anybody give me a hint or a code how this should look like? And furthermore: If this is correct: how can i make this AND let the arduino do other things like reading another sensor. If i understand the concept of loops and whiles, the arduino won't do anything while the loop is in progress. This is why i do not understand how to structure the sketch..

thank you so much!

sl


AWOL

Let us all intone the mantra:
Look at the Blink Without Delay example, without delay,
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sublevel

ok. I understand. Or at least i try to. Unfortunately i do not understand, how i can use this for my example where 3 different options (depending on sensor values) are available. could you please help?

thanks

sl

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grag38

Remove the first delay(4000); its not important in this case

It is slowing the effect because it a wait of 4 seconds !

Play with delay values to get the effect you want.

sublevel

ok...do you mean this way? (i removed the third else if condition for better reviewing)

thank you!
Code: [Select]


int SensorA = 0;
const int ledPin =  2;      // the number of the LED pin
int sensor_val;


// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long previousMillis2 = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)
long interval2 = 7000;           // interval at which to blink (milliseconds)

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);     

}
void loop()
{
 
// code 2b run all the time

sensor_val = analogRead(SensorA);
Serial.print("Value from sensor");
Serial.println( sensor_val );


if (sensor_val > 850)
{
Serial.print("not enough");
}   
 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

else if ((sensor_val < 850) && (sensor_val > 500))

{     
Serial.print("good");
}

unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = currentMillis2;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

}






AWOL

No, I was thinking of something much simpler: (uncompiled, untested)
Code: [Select]
const int SensorA = 0;
const int ledPin =  2;      // the number of the LED pin

int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  int sensor_val = analogRead(SensorA);
  Serial.print("Value from sensor");
  Serial.println( sensor_val );

  if (sensor_val > 850){
    Serial.print("not enough");
    interval = 100;
  } else if ((sensor_val < 850) && (sensor_val > 500)) {
    Serial.print("good");
    interval = 500;
  }
 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    ledState = 1 - ledState;
    digitalWrite(ledPin, ledState);
  }
}


Not sure exactly what blink rate you want, but hopefully, you get the idea.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sublevel

omg. I think i got it and i have to admit: that's (of course) smarter! Thank you so much!!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy