When i run my code and the slideSwitch is off the servo goes to 90 degree

hello,
I have a couple of problems and before i descript , i used TinkerCad to build my project.

So like i mention in title , when i run the code and the slideSwitch is off the servos goes to 90 degree , this the first problem

the second is i connect LCD Screen 16*2 to show the the state of code , but the message blink continuously

// include the librarys
#include <LiquidCrystal.h>
#include <Servo.h>

// create an object for servo motor
Servo baseMotor;
Servo handlerMotor;

// Define variables
int basePotine;
int handlerPotine;
int SlideSwitch = 2;

// LiquidCrystal 16*2
LiquidCrystal lcd(3,4,5,6,7,8);   // (RS,E,D4,D5,D6,D7)


void setup()
{
  baseMotor.attach(11); // connect the object with the pin
  handlerMotor.attach(12);
  lcd.begin(16,2); // (C,R) start the lcd screen
  pinMode(SlideSwitch,INPUT_PULLUP); // connect the slideSwitch with the pin

}

void loop()
{
  if(digitalRead(SlideSwitch) == 1){    
    lcd.clear(); // clear the lcd
    basePotine = analogRead(A0); // read the signal come from the analogPin
    basePotine = map(basePotine,0,1023,0,180); // change the value from 0 - 1023 to to 0 degree - 180 degree
    baseMotor.write(basePotine); // move the motor to the new angler
    
    handlerPotine = analogRead(A1);
    handlerPotine = map(handlerPotine,0,1023,0,90); //change the value from 0 - 1023 
    handlerMotor.write(handlerPotine); 
    
    lcd.setCursor(0,0);
    lcd.print("base dgree:");
    lcd.setCursor(11,0);
    lcd.print(basePotine);
        
    lcd.setCursor(0,1);
    lcd.print("handler dgree:");
    lcd.setCursor(14,1);
    lcd.print(handlerPotine);
    delay(50);
  }
  else{
    // if the switch is off the servo will stop 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("System off");
    delay(50);
  }
}

and i post a GIF to see the project , thanks
project 2.0

The slide switch shorts power to ground? the image does not enlarge well.

attach() sends a servo to the default of 90 if you don't tell it to go somewhere else. And if the switch is off you never write() anything to the servos. Where do you expect or hope that they will go to?

Steve

That is because every time round the loop you clear the display and then write to it again. This causes the blinking. To stop this only clear and display when the information you want to display changes.


i hope you can see the wires

You do realize that if the state of the switch is 0, the LCD will clear the screen, put the cursor at 0, print a message, delay 50ms, and then clear the screen, put the cursor at 0, print a message, delay 50ms, , and then clear the screen, put the cursor at 0, print a message, delay 50ms, , and then clear the screen, put the cursor at 0, print a message, delay 50ms<<< is that what issue you are seeing?
Have you considered a few screen prints to see what's going on?

that the first time i know attach function send servo to a default degree , I expected it just connected to Arduino

I just want the servo waits until the the slide switch is on and potentimeter change the angle

so right after the attachment is made send the servos to the desired startup position.

yes that what happen

What's the problem?

Then don't do the attach() until the switch is on. And read the potentiometer, do the map() and the first servo write() BEFORE the attach(). I know it sounds odd but it will work.

Steve

Yes that is correct, but remember that once the servos are attached don’t do the attach again. Use a Boolean variable to prevent you attaching them more than once.

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