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:
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..
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?
ok...do you mean this way? (i removed the third else if condition for better reviewing)
thank you!
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);
}
}
No, I was thinking of something much simpler: (uncompiled, untested)
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.