AnalogWrite not working in If Statement

Arduino Uno with ATMega328P Processor.AnalogWrite not working properly in If statement. It changes the brightness of Led like there isnt any if statements at all. There are 2 potentiometers to create an intervall in which to program increases the brightness of led starting 40% of maxi. When its lower then min value led is closed.

int ledPin = 6;
int sensorPot = 1; // its 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);


void loop() {

  int start = analogRead(minPot);
  int maxVal = analogRead(maxPot);
  int sensor = analogRead(sensorPot);
if(sensor/4 > start/4){
else if(sensor = baslangic){
else if ((start < sensor < maxVal)) {
  brightness = sensor/4 - fortypercent;
  }else if (sensor = maxVal){
    analogWrite(ledPin , 255);


That's interesting (and unlikely) - what proof do you have?

Arduino Uno with ATMega328P Processor cant execute AnalogWrite in If statement.

How do you know? What actually happens?

Post a sample of the output when you run the program.



Code: [Select]

analogWrite(analogOutPin, LOW);

Code: [Select]

analogWrite(analogOutPin, 0);


You need to look at the source of analogWrite for the AVR processors, particularly for an argument of zero (aka LOW) :wink:


else if(sensor = baslangic){

That is an assignment statement, not a comparison. = != ==

else if ((start < sensor < maxVal)) {

That is NOT how to test that a value is in a range. That is testing that start < sensor, which is either true or false, is less than maxVal. Unless maxVal is less than 2, that statement will always be true.