Code not woking correctly and i dont know why it wont run the nested if statements

/*
Simple Code but it wont run the nested if statements.
ive stripped the origional code down to just this small piece present in the void loop but it still wont run and im stuck. sorry for the lack of explaination but it is 4;06 in the morning and not even my teacher can help me
*/

#include "Display.h"
const int Light_Reciever = A2;
const int RED_LIGHT = 4;
const int GREEN_LIGHT = 5;
const int BLUE_LIGHT = 6;
const int YELLOW_LIGHT = 7;
const int KEY_1 = 8;
const int KEY_2 = 9;
const int PIN_POTMETER = 14;
const int PIN_LDR = 16;
float lux;
int mode = 0;
int angle;
const int PIN_NTC = 15;
const int NTC_R25 = 10000; // the resistance of the NTC at 25'C is 10k ohm
const int NTC_MATERIAL_CONSTANT = 3950; // value provided by manufacturer
// the maximum angle in degrees of the potmeter 0-280
////int lastbuttonstate = HIGH;
////int buttonstate = HIGH;
int LAST_KEY1_STATE = HIGH;
int LAST_KEY2_STATE = HIGH;
int lasttime = 0;
int LAST_RIGHT_BUTTON_STATE = HIGH;
int LAST_LEFT_BUTTON_STATE = HIGH;
int LEFT_BUTTON_STATE = LOW;
int RIGHT_BUTTON_STATE = LOW;
void setup()
{
pinMode(PIN_POTMETER, INPUT);
pinMode(RED_LIGHT, OUTPUT);
pinMode(YELLOW_LIGHT, OUTPUT);
pinMode(GREEN_LIGHT, OUTPUT);
pinMode(BLUE_LIGHT, OUTPUT);
pinMode(Light_Reciever, INPUT);
pinMode(KEY_1, INPUT_PULLUP);
pinMode(KEY_2, INPUT_PULLUP);
pinMode(PIN_LDR, INPUT);
pinMode(PIN_POTMETER, INPUT);
Serial.begin(9600);
}

int get_knob_angle()
{
int sensor_value = analogRead(PIN_POTMETER);

// map is an Arduino library function.
angle = map(sensor_value, 0, 1023, 0, 100); // it maps one range to another range.

}
int GET_LIGHT_LEVEL()
{
int sensorValue = analogRead(PIN_LDR);
float resistance_sensor;
// and convert to resistance in Kohms
resistance_sensor = (float)(1023 - sensorValue) * 10 / sensorValue;

//Serial.print("The resistance of the light sensor is: ");
// Serial.print(resistance_sensor, 1);
// Serial.println(" KOhm");

// convert the resitance to Lux

lux = 325 * pow(resistance_sensor, -1.4);
}
float get_temperature()
{
float temperature, resistance;
int value;
value = analogRead(PIN_NTC);
resistance = (float)value * NTC_R25 / (1024 - value); // Calculate resistance
/* Calculate the temperature according to the following formula. */
temperature = 1 / (log(resistance / NTC_R25) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
return temperature;
}
void loop()
{

int wheel_position = get_knob_angle();
int light = GET_LIGHT_LEVEL();
RIGHT_BUTTON_STATE = digitalRead(KEY_1);
LEFT_BUTTON_STATE = digitalRead(KEY_2);
Display.show(angle);
//Display.show(lux);

if (lux < 10)
{
digitalWrite(YELLOW_LIGHT, HIGH);
}
else
{
digitalWrite(YELLOW_LIGHT, LOW);
}
millis();

if ( LEFT_BUTTON_STATE == LOW)
{

digitalWrite(GREEN_LIGHT, LOW);
digitalWrite(BLUE_LIGHT, HIGH);
Serial.println(angle);
millis();
if (angle < 40 )
{
  digitalWrite(BLUE_LIGHT, LOW);
  digitalWrite(GREEN_LIGHT, LOW);
  millis();
  if (angle > 45 )
  {

    mode = 0;
  }
}

}
millis();

}

Please edit your post and use code tags. These things... </>.

This doesn’t do anything for you:

millis();



if (angle < 40 )
{
  digitalWrite(BLUE_LIGHT, LOW);
  digitalWrite(GREEN_LIGHT, LOW);
  millis();
  if (angle > 45 )
  {

    mode = 0;
  }
}

If angle was < 40 then how can it be > 45 ? :thinking:

@34626 ask your teacher about these:

  millis();

Random calls to millis() that are carefully placed strewn about in your code.

As written, they do... nothing except take a tiny bit of time. And a tiny bit of program space.

a7

Hi, @34626
To add code please click this link;

Please repost your code in a new post. Please..

What is your code supposed to do?
What does it do?

Have you written your code in stages, to test each piece of hardware BEFORE combining your code?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

If angle <40 it cant be bigger than 45

main idea is that the potentiometer is supposed to go below 40 to turn the light of and then go above 45 to to run the next piece of code but this particular part wont run.

When you detect <40 turn OFF your light and make a Flag variable = true.

When the Flag variable is true and you detect >45 then you run the next piece of code.

You have nested the if statements.
Your code

if (angle < 40)
{
  do something 
  if (angle > 45)
  {
    do something else
  }
}

The 2nd if statement can never be true because it only gets executed when the first if statement is true. You need to separate them.

if (angle < 40)
{
  do something
} 
if (angle > 45)
{
  do something else
}

Do you see the difference?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.