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);
}
}
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?
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.
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?
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.
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.