Hello, as part of my studies we are doing arduino. I have got most of the code sorted but I need to make "LED1" switch on with a potentiometer at 75% and off again anything under this. The second pot needs to turn the same light on at 60% and off anything under this.
The problem I have is with the 2nd LED:
It needs to turn on with a 1 sec delay when either Pot reaches its setpoint (75 and 60%). This I have working.
The LED needs to turn off when the first pot goes under 75%.
I cannot get the code right! I have included it below, any pointers would be much appreciated.
const int pushButton = 3; // the number of the pushbutton pin
const int pushButtonLED = 2; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
float sensorPin0 = A0; // select the input pin for the potentiometer
float sensorPin2 = A2; // select the input pin for the potentiometer
int led1 = 7; // select the pin for the BLUE LED
int led2 = 6; // select the pin for the RED LED
int led1State = 0; // variable for reading the pushbutton status
int led2State = 0; // variable for reading the pushbutton status
float sensorValue0 = 1; // variable to store the value coming from the sensor
float sensorValue2 = 2; // variable to store the value coming from the sensor
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(pushButton, INPUT);
pinMode(pushButtonLED, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode (sensorPin0, INPUT);
pinMode(sensorPin2, INPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
int buttonState = digitalRead(pushButton);
if (buttonState == HIGH)
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(pushButtonLED, HIGH);
delay(200);
digitalWrite(pushButtonLED, LOW);
delay(200);
}
else {
// turn LED off:
digitalWrite(pushButtonLED, LOW);
sensorValue0 = analogRead(sensorPin0);
if(sensorValue0 >=767)
{
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led2, HIGH);
}
sensorValue0 = analogRead(sensorPin0);
if(sensorValue0 <767)
{
digitalWrite(led1, LOW);
}
sensorValue2 = analogRead(sensorPin2);
if(sensorValue2 >=613)
{
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led2, HIGH);
}
sensorValue2 = analogRead(sensorPin2);
if(sensorValue2 <613)
{
digitalWrite(led1, LOW);
}
sensorValue0 = analogRead(sensorPin0);
if(sensorValue0 <767)
{
digitalWrite(led2, LOW);
}
float sensorValue0 = analogRead(A0);
Serial.print((sensorValue0/1023)*20);
delay(1); // delay in between reads for stability
float sensorValue2 = analogRead(A2);
Serial.print((sensorValue2/1023)*20);
delay(1); // delay in between reads for stability
int led1State = digitalRead(led1);
Serial.print(led1State);
delay(1); // delay in between reads for stability
int led2State = digitalRead(led2);
Serial.print(led2State);
delay(1); // delay in between reads for stability
int pushButtonLED = digitalRead(pushButton);
Serial.print(pushButtonLED);
delay(1);
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.