Pages: [1]   Go Down
Author Topic: Beginner Question: Blinking LED when sensor value = xyz / Loop?  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26522
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let us all intone the mantra:
Look at the Blink Without Delay example, without delay,
Logged

"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.

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

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26522
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just change the value of "interval"
Logged

"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.

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

thank you!
Code:

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);
  }

}




Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26522
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I was thinking of something much simpler: (uncompiled, untested)
Code:
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.
Logged

"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.

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

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

Pages: [1]   Go Up
Jump to: