I have a project, so I have to make a fan that has 2 modes:
1- Automatic according to feedback from a temperature sensor
2- Manual, which is like: control the fan speed or turn it off whenever you want.
I'm having and issue with the slide switch that changes between the 2 modes.
My code is running well.
But the switch is not working.
Turns out I'm not connecting the switch correctly, but I don't know how to connect it correctly
int switchA = 6;
int Motor = 3;
int Potentiometer = 1;
void setup()
{
Serial.begin(9600);// So I can use the serial monitor
//declaring the led outputs
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
//declaring the fan motor output
pinMode(3, OUTPUT);
//the temp sensor input
pinMode(0, INPUT);
//Switch input pin
pinMode(6, INPUT);
//Potentiometer Input pin
pinMode(1, INPUT);
Serial.print("voltage:");
Serial.print("\t");
Serial.println("deg C:");
}
void loop()
{
Rankleds();//made a seperate function for the leds
Fanmotor();// This function is for the fan motor
}
void Rankleds()//Void for the leds
{
for(int ledpin=10; ledpin<=13; ledpin++)
/*Used a for loop to make things easier and shorter,
int ledpin=10 is initialisation
ledpin<=13 is conditiom
ledpin++ is increment
*/
{
digitalWrite(ledpin, HIGH);//Turn Led ON
delay(750);// Wait 0.750 seconds
digitalWrite(ledpin, LOW);// Turn Led OFF
}
/*
so a simple explanation: in this for loop,
it turns on the first led on pin 10 then waits 0.750 seconds
then turns it off then moves to the next led
which is on pin 11 and does the same thing until it finishes
at the led in pin 13, then goes in a loop
*/
}
void Fanmotor()
{
float voltage, degreesC;
voltage = getVoltage();
degreesC = (voltage - 0.5)*100.0;
Serial.print(voltage);
Serial.print("\t");
Serial.println(degreesC);
delay(1000);
//-------------------
if (switchA == HIGH)
{
if(degreesC >= 22.0)
{
analogWrite(Motor, 255);
delay(20);
}
else if(degreesC < 22.0)
{
analogWrite(Motor, 0);
delay(20);
}
}
else if(switchA == LOW){
int value = analogRead(Potentiometer);
float speed1 = value/1023 * 255;
analogWrite(3, speed1);
}
//---------------------
}
float getVoltage()
{
int reading = analogRead(0);
float voltage = reading * 5.0/1024.0;
return voltage;
}
is wrong, it has to be like this:
int potentiometer = A0;
This is not required because you can use the potentiometer with:
int value = analogRead(Potentiometer);
reads. the Arduino knows that it has to switch to analog input here.
This is not reading the position of the switch, you need something like:
if (digitalRead(switchA) == HIGH)
similarly:
else if(switchA == LOW)
Needs changing in a similar manner.
Here you are checking whether "switchA" is equal to zero (which is what LOW means), but it can never be, because it is a pin number (which you have set to be 6):
// C++ code
//
int switchA = 6;
int Motor = 3;
int Potentiometer = 1;
void setup()
{
Serial.begin(9600);// So I can use the serial monitor
//declaring the led outputs
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
//declaring the fan motor output
pinMode(Motor, OUTPUT);
//the temp sensor input
pinMode(0, INPUT);
//Switch input pin
pinMode(switchA, INPUT);
//Potentiometer Input pin
pinMode(Potentiometer, INPUT);
Serial.print("voltage:");
Serial.print("\t");
Serial.println("deg C:");
}
void loop()
{
Rankleds();//made a seperate function for the leds
Fanmotor();// This function is for the fan motor
}
void Rankleds()//Void for the leds
{
for(int ledpin=10; ledpin<=13; ledpin++)
/*Used a for loop to make things easier and shorter,
int ledpin=10 is initialisation
ledpin<=13 is conditiom
ledpin++ is increment
*/
{
digitalWrite(ledpin, HIGH);//Turn Led ON
delay(750);// Wait 0.750 seconds
digitalWrite(ledpin, LOW);// Turn Led OFF
}
/*
so a simple explanation: in this for loop,
it turns on the first led on pin 10 then waits 0.750 seconds
then turns it off then moves to the next led
which is on pin 11 and does the same thing until it finishes
at the led in pin 13, then goes in a loop
*/
}
void Fanmotor()
{
float voltage, degreesC;
voltage = getVoltage();
degreesC = (voltage - 0.5)*100.0;
Serial.print(voltage);
Serial.print("\t");
Serial.println(degreesC);
delay(1000);
//-------------------
if (digitalRead(switchA)==HIGH)
{
if(degreesC >= 22.0)
{
analogWrite(Motor, 255);
delay(20);
}
else if(degreesC < 22.0)
{
analogWrite(Motor, 0);
delay(20);
}
}
else if(digitalRead(switchA) == LOW){
int value = analogRead(Potentiometer);
float speed1 = (value/1024) * 255;
analogWrite(3, speed1);
}
Serial.println(digitalRead(switchA));
//---------------------
}
float getVoltage()
{
int reading = analogRead(0);
float voltage = reading * 5.0/1024.0;
return voltage;
}
The issue left is the manual mode. The switch turns on the automatic mode according to the temp sensor. But if I switch the switch the potentiometer mode (manual mode) isn't working.
Is it my code or the way I'm connecting the switch?