Issues with connecting a slideswitch to change between automatic and manual mode for a fan

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

here is my circuit:

here is my code( forget about void Rankleds() ):

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;
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Where is the switch in that diagram ?

Maybe I have missed it, but you do not appear to read the switch.
I'd have expected a digitalRead( 6 ) somewhere.

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.

Your code is not reading the switch input.

I made a digitalRead(switchA); nothing changed

Make your code more readable by instead of doing, for example, something like this:

//Switch input pin
  pinMode(6, INPUT);

Do this:
pinMode(switchA, INPUT);

Then post the entire code

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):

int switchA = 6;

Thank you so much, But the potentiometer mode is not working

But I connected the potentiometer on A1

I tried to make
int potentiometer = A1;

it didn't work

Please post your latest code so we can see any changes you have made.

// 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?

This needs attention.

float speed1 = (value/1024.0) * 255.0 ; /****/ // with float, use .0 on integers

IT WORKED, Perfecttt, Thank you so muchhhh

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