hello, very new to programming and arduino.
i got the below scetch working fine, but what if i want to turn led1 on between 100-200
and led2 200 - 250? i need a "inbetween" value
and what about value being exactly 200 or 201?
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int ledPin1 = 10;
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
{
if(sensorValue < 200)// what can i write here? i need a between value? how do i do that?
digitalWrite(ledPin, HIGH); // turn on LED
else
digitalWrite(ledPin, LOW); // turn off LED
}
if(sensorValue > 200)
digitalWrite(ledPin1, HIGH); // turn on LED
else
digitalWrite(ledPin1, LOW); // turn off LED
}
thank you so much =) so praud of myself for getting this tiny (may not be the best code) working.
one small victory for me, thank you =)
tbh it don*t have much to do with covid-19 but i could not figure out wich sub forum was the correct place.
(and yes ive read the newbie stickies)
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int ledPin1 = 10;
const int ledPin2 = 11;
const int ledPin3 = 12;
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
}
{
if ((50 < sensorValue) and (100 > sensorValue))
digitalWrite(ledPin, HIGH); // turn on LED
else
digitalWrite(ledPin, LOW); // turn off LED
}
{
if ((100 < sensorValue) and (150 > sensorValue))
digitalWrite(ledPin1, HIGH); // turn on LED
else
digitalWrite(ledPin1, LOW); // turn off LED
}
{
if ((150 < sensorValue) and (200 > sensorValue))
digitalWrite(ledPin2, HIGH); // turn on LED
else
digitalWrite(ledPin2, LOW); // turn off LED
}
{
if (200 < sensorValue)
digitalWrite(ledPin3, HIGH); // turn on LED
else
digitalWrite(ledPin3, LOW); // turn off LED
}
{
Serial.println(sensorValue);
}
}
Here is a way to include the values that are exactly 100, 150, and 200. Also you had a lot of extra braces creating unnecessary code blocks.
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int ledPin1 = 10;
const int ledPin2 = 11;
const int ledPin3 = 12;
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
if ((50 < sensorValue) and (100 >= sensorValue))
digitalWrite(ledPin, HIGH); // turn on LED
else
digitalWrite(ledPin, LOW); // turn off LED
if ((100 < sensorValue) and (150 >= sensorValue))
digitalWrite(ledPin1, HIGH); // turn on LED
else
digitalWrite(ledPin1, LOW); // turn off LED
if ((150 < sensorValue) and (200 >= sensorValue))
digitalWrite(ledPin2, HIGH); // turn on LED
else
digitalWrite(ledPin2, LOW); // turn off LED
if (200 < sensorValue)
digitalWrite(ledPin3, HIGH); // turn on LED
else
digitalWrite(ledPin3, LOW); // turn off LED
Serial.println(sensorValue);
}