Arduino Uno with ATMega328P Processor.analogWrite
is not working properly in if statement. It changes the brightness of the LED as if there wasn't any if statement at all.
There are 2 potentiometers to create an intervall in which to program increases of the LED brightness, starting at 40% of the maximum. When it's lower than the minimum value, the LED is turned off.
int ledPin = 6;
int sensorPot = 1; // it's a potentiometer for now
int maxPot = 7; // potentiometer
int minPot = 3; // potentiometer
int fortypercent = 255*0.4;
int brightness = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPot, INPUT);
pinMode(maxPot, INPUT);
pinMode(minPot, INPUT);
Serial.begin(9600);
}
void loop() {
int start = analogRead(minPot);
int maxVal = analogRead(maxPot);
int sensor = analogRead(sensorPot);
Serial.println(sensor/4);
Serial.println(start/4);
Serial.println(maxVal);
if(sensor/4 > start/4) {
analogWrite(ledPin,sensor/4);
} else if(sensor = baslangic) {
analogWrite(ledPin,LOW);
} else if ((start < sensor < maxVal)) {
brightness = sensor/4 - fortypercent;
analogWrite(ledPin,brightness);
} else if (sensor = maxVal) {
analogWrite(ledPin , 255);
}
}
-
1Can't execute how? What happens?jose can u c– jose can u c2018年04月09日 20:01:30 +00:00Commented Apr 9, 2018 at 20:01
-
1Of course it can. What problem are you having? What output do you expect from this program? What output do you have instead?Edgar Bonet– Edgar Bonet2018年04月09日 20:04:10 +00:00Commented Apr 9, 2018 at 20:04
-
Sorry, edited questionkeser– keser2018年04月09日 20:04:56 +00:00Commented Apr 9, 2018 at 20:04
-
Please, be more specific, like "If I set the pot XXX to position XXX I expect the program to do XXX and instead ot does XXX".Edgar Bonet– Edgar Bonet2018年04月09日 20:07:04 +00:00Commented Apr 9, 2018 at 20:07
-
I updated my questionkeser– keser2018年04月09日 20:22:48 +00:00Commented Apr 9, 2018 at 20:22
1 Answer 1
ORIGINAL RESPONSE, BASED ON DIFFERENT CODE
You are comparing sensorValue
to see if it's ever less than minValue
.
However, you have mapped sensorValue
to range from 0
to 255
, and minValue
is set at 0
. How can anything from 0-255 be less than zero?
NEW RESPONSE, BASED ON UPDATED CODE
One of your if
statements: else if(sensor = baslangic)
is not comparing, but assigning baslangic
to sensor
. To compare, use ==
.
Your other if
statement: else if ((start < sensor < maxval))
is wrong. C++ doesn't work this way. Maybe you want: if ((start<sensor) && (sensor<maxval))
?
Instead, your statement evaluates like if ( (start<sensor) < (maxval) )
, which means you are comparing the TRUE
or FALSE
of (start<sensor)
to the int
that is maxval
.
-
I am analog reading minValue.keser– keser2018年04月09日 20:08:05 +00:00Commented Apr 9, 2018 at 20:08
-
Indeed. So, what is connected to pin
A3
(minPot) and what values fromanalogRead()
is it providing? Use debug statements. I will retract this answer, since I misread the question.jose can u c– jose can u c2018年04月09日 20:10:08 +00:00Commented Apr 9, 2018 at 20:10 -
Sorry for asking not well explained questions... Newbie here... Updated the questionkeser– keser2018年04月09日 20:22:04 +00:00Commented Apr 9, 2018 at 20:22