I am having trouble on writing the code to make my servo motor ( Dusco.E Micro Servo SG90 ) to rotate while using the myoware muscle sensor. my goal is to have the servo motor turn 120 degrees when flexing then return those 120 degrees when not flexing. I have this is an easy code but for some reason I cannot get the servo to turn back.
Can't see your code or your schematic
Here is the code. super simple just trying to test one servo for a robotic hand but cant get the servo to go back to a different position after it rotates once.
#include <Servo.h>
Servo servo_1; // servo number 1
void setup ()
{
Serial.begin(9600);
servo_1.attach(9);
}
void loop()
{
Serial.println(analogRead(0));
if( analogRead(A0)> 600 )
{
servo_1.write(100);
}
else ( analogRead(A0)< 400 );
{
servo_1.write(50);
}
delay(100);
}
with codes tags (like your supposed to post it)
#include <Servo.h>
Servo servo_1; // servo number 1
void setup (){
Serial.begin(9600);
servo_1.attach(9);
}
void loop(){
Serial.println(analogRead(0));
if( analogRead(A0) > 600){
servo_1.write(100);
}else(analogRead(A0) < 400){
servo_1.write(50);
}
delay(100);
}
And your code compiled/worked.... as YOU posted it?
(with the semi-colon after the else conditional?)
yes the code compiled as i wrote it. This code should not be too complicated. I did not see I had to use code tags. I also added some notes to the code but I'm assuming everyone should know what this simple code should do just cant get the servo to rotate back or rotate to the new position.
#include <Servo.h>
Servo servo_1; // servo number 1
void setup ()
{
Serial.begin(9600); // initialize serial communication at 9600 bits per second
servo_1.attach(9); // servo is attached to D9
}
void loop()
{
Serial.println(analogRead(0)); // read the input on A0
if( analogRead(A0)> 600 ) // if the input values from sensor are above 600
{
servo_1.write(100); // tells servo to go to this position
}
else ( analogRead(A0)< 400 ); // if the input values from sensor are below 400
{
servo_1.write(50); // tells servo to go to this position
}
delay(100); // this is to slow down the input values to 100ms
}
else ( analogRead(A0)< 400 );
Re-read reply#3
if I do not have the ' ; ' after the else statement it will not run for some reason.
"run", or compile?
here is the error. this is my first time using arduino to program and im using an arduino nano. I have programmed in MATLAB and thats it.
You're missing another "if".
(Code that doesn't compile cannot run)
wow. so it doesnt work with else if statements? thanks very simple fix now to make a more complex code for a robotic hand.
so it doesnt work with else if statements?
You never had an "else if" (read back what you wrote)
#include <Servo.h>
Servo servo_1; // servo number 1
void setup (){
Serial.begin(9600);
servo_1.attach(9);
}
void loop(){
Serial.println(analogRead(0));
if(analogRead(A0) > 600){
servo_1.write(100);
}else if (analogRead(A0) < 400){
servo_1.write(50);
}
delay(100);
}
Hi,
Welcome to the forum.
Tom...