Write an Arduino sketch on Tinkercad that reads a single line from the keyboard as follows:
If the input is
L:120
The servo motor will rotate 120 degrees to the left
If the input is
R:90
The servo will rotate by 90 degrees to the right
Write an Arduino sketch on Tinkercad that reads a single line from the keyboard as follows:
If the input is
L:120
The servo motor will rotate 120 degrees to the left
If the input is
R:90
The servo will rotate by 90 degrees to the right
Welcome to the forum
Is this an assignment that you want us to do for you ?
What have you written so far ?
Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
What will the servo doof it’s already at or near the limit of travel, and gets a command to go further ?
What will the servo do if it’s already at . . .
Hehe… along with the doof noise when it hits the limit !
Thanks
Read the first character into a direction variable
Skip the second character
Parse the rest of the string into an integer
Move the servo
Can you please tell us how much experience you have with Arduinos and C++?
I tried to write something in it, but I could not complete it. I had difficulty writing the code to enter the angle and direction of rotation with each other via the keyboard
#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int poser = 0; // initial position of server
int val; // initial value of input
void setup() {
Serial.begin(9600); // Serial comm begin at 9600bps
ser.attach(9);// server is connected at pin 9
}
void loop() {
if (Serial.available()) // if serial value is available
{
val = Serial.read();// then read the serial value
if (val == 'R') //if value input is equals to d
{
poser += 1; //than position of servo motor increases by 1 ( anti clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
if (val == 'L') //if value input is equals to a
{
poser -= 1; //than position of servo motor decreases by 1 (clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
}
}
Thank you for posting your code BUT
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
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.