0

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);
  }
}
5
  • 1
    Can't execute how? What happens? Commented Apr 9, 2018 at 20:01
  • 1
    Of course it can. What problem are you having? What output do you expect from this program? What output do you have instead? Commented Apr 9, 2018 at 20:04
  • Sorry, edited question
    – keser
    Commented 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”. Commented Apr 9, 2018 at 20:07
  • I updated my question
    – keser
    Commented Apr 9, 2018 at 20:22

1 Answer 1

4

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.

3
  • I am analog reading minValue.
    – keser
    Commented Apr 9, 2018 at 20:08
  • Indeed. So, what is connected to pin A3 (minPot) and what values from analogRead() is it providing? Use debug statements. I will retract this answer, since I misread the question. Commented Apr 9, 2018 at 20:10
  • Sorry for asking not well explained questions... Newbie here... Updated the question
    – keser
    Commented Apr 9, 2018 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.