Hey, I'm a bit new to Arduino (a whopping veteran of two weeks), so keep that in mind, and forgive any stupid mistakes/questions of mine. Thank you in advance for your help!
So a few days ago, I hand crafted my very own NanoMouse with an Arduino Nano, two servos and an omniball. I tried a few simple commands such as myservo.writeMicroseconds(1700) just to see if the servos were working, and all was well. Just last night, I decided I wanted to make my mouse controllable by WASD, the keyboard buttons. If I enter one "if" statement, the input works fine, but if I add one or more "else if" statements, it just continues following the first if statement no matter what. For example, if my first if statement was
if (incomingByte=115){
leftServo.writeMicroseconds(1300);
rightServo.writeMicroseconds(1700);
delay(1000);
stop();
, the next statement, in this case, an "else if" statement, would follow the servo directions for this first if statement no matter what. Here is my code so far. I am also decently new to the forums, so please excuse me if I either missed a post that was already on this subject, or I didn't put this post in the right section. Feel free to ask for more details if I wasn't clear, and thank you again for your help.
#include <Servo.h>
Servo leftServo;
Servo rightServo;
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
leftServo.attach(6);
rightServo.attach(5);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
if (incomingByte=115){
leftServo.writeMicroseconds(1300);
rightServo.writeMicroseconds(1700);
delay(1000);
stop();
}
else if (incomingByte=119){
leftServo.writeMicroseconds(1700);
rightServo.writeMicroseconds(1300);
delay(1000);
stop();
}
else if (incomingByte=100){
leftServo.writeMicroseconds(1300);
rightServo.writeMicroseconds(1300);
delay(100);
stop();
}
else if (incomingByte=97){
leftServo.writeMicroseconds(1700);
rightServo.writeMicroseconds(1700);
delay(100);
stop();
}
}
}
void stop(){
leftServo.writeMicroseconds(1500);
rightServo.writeMicroseconds(1500);
delay(500);
}