/*
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();
}