good morning,
I would need help in making a servo motor move with the if function, I wanted that when a led turned on the motor would move a few degrees, I have already tried with the code I have attached, but the servo motor does not work, could you help me ?
#include <Servo.h>
#define LED 13 //relè
bool state = LOW;
char getstr;
int pos = 0;
Servo servo;
void setup(){
pinMode (7, OUTPUT);
servo.attach(3);
Serial.begin (9600);
}
void stateChange() {
state = !state;
digitalWrite(LED, state);
delay (5000);
digitalWrite(LED, LOW);
}
void servo1(){
if (LED == HIGH){
servo.write (90);
}
}
void loop() {
getstr = Serial.read();
Serial.print(getstr);
switch(getstr){
case 'r': servo1(); break;
case 'a': stateChange(); break;
default: break;
}
}
if (digitalRead (LED))
servo.write (90);
else
servo.write (<some other angle>) ;
You need to read the pin, and then use the bool returned to choose what to do. You need to write different values to the servo or it'll stay put forever at 90 degrees...
But I'm a little confused by your question... your code seems to try to turn the Led on, but you said you wanted the motor to move when the led is turned on (I'm interpreting this as you having a sensor for an led seperate from your Arduino).
For the latter, refer to @MarkT 's reply. If you want to change the state of your led by typing a key into the serial console and then have another key to update the motors position based on that state, do this:
void stateChange() {
state = !state;
digitalWrite(LED, HIGH);
delay (5000);
digitalWrite(LED, LOW);
}
void servo1() {
if (state)
servo.write(90);
else
servo.write(<some other angle>);
}
void loop() {
getstr = Serial.read();
Serial.print(getstr);
switch(getstr){
case 'r': servo1(); break;
case 'a': stateChange(); break;
default: break;
}
}
If you don't check to see if there is input in the buffer and there isn't, Serial.read() will return -1 which will print as a strange character. Most people put an "if (Serial.available())" around the part that processes input characters.
Good morning,
I can't get the servo motor to turn, I would need the servo motor to turn when the LED is on, and when it is off, it returns to the starting point, can you help me?
if you find it useful, it is the modification of the code that was in the smart robot car v. 4 by elegoo. at the end of the code there are the codes to be able to interact with the mobile phone via bluetooth
thank you very much for helping!
#include <Servo.h>
#define LED 13 //relè
bool state = LOW; /////// Should be true or false
int getstr; /////// Serial.read() returns an int. -1 if no input available.
int pos = 0; ////// You never use this variable.
Servo servo;
void setup()
{
pinMode (7, OUTPUT); ////// You never use Pin 7
pinMode(LED, OUTPUT); /////// You forgot to set the LED pin as OUTPUT
servo.attach(3);
Serial.begin (9600);
}
void stateChange()
{
state = !state;
// "I wanted that when a led turned on the motor would move a few degrees,"
if (state)
servo.write(93); // Default servo start position is 90
digitalWrite(LED, state);
delay (5000);
digitalWrite(LED, LOW);
}
void servo1()
{
if (LED == HIGH) ///// This is never true. LED is 13 and HIGH is 1
{
servo.write (90);
}
}
void loop()
{
getstr = Serial.read();
if (getstr != -1) /////// Serial.read() returns an int. -1 if no input available.
Serial.print(getstr);
switch (getstr)
{
case 'r': servo1(); break;
case 'a': stateChange(); break;
default: break;
}
}