Hi all,
I am fairly new to arduino, and BRAND NEW to this forum, so hi, and apologies if this is in the wrong place or if I am doing anything wrong.
I am working on a project which is basically an arm (built of cardboard and 3 x Servos), with a stylus in the end, which I want to move to each letter on a physical keyboard, depending on what is typed in the Serial.
There are a whole bunch of other parts to this (I intend to use a GSM Shield to be able to text a string to the serial, which will then type out on the RL keyboard.
I know the concept seems odd and long winded, but this is my target so bare with me.
The current issue I am having; is that regardless of which letter is typed, it ends up running "a". I assume its a problem with my IF statement or perhaps I should be using a different statement for this? Yes I plan on doing the whole alphabet once i can get it to choose between the first two letters.
I have commented a bunch of the code to simplify to one servo while i fault find currently.
As the code is... I want the pan servo to turn to 43 degrees if an "a" is written, and 180 degrees if a "b" is written.
Any help is much appreciated, and I will try and answer any questions I have.
Thanks in advance, code below.
Spice
#include <Servo.h>
Servo pan;
Servo tilt;
Servo tap;
String msg = "What do you want to type?"; //Serial user message
String userInput = ""; //holder for the inputted string
char userInputArray[4]; //conversion of string to array
char letter1;
char letter2;
char letter3;
int posPan;
int posTilt;
int posTap;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pan.attach(9);
tilt.attach(10);
tap.attach(11);
pan.write(90); //intialise servos to center
tilt.write(90);
tap.write(90);
}
void loop() {
// put your main code here, to run repeatedly:
letter1=""; //initialise letter1
Serial.println(msg);
while (Serial.available()==0){
} //Ask for input
getUserInput(); //attach string to letters
letter1Select(); //find out which letter is in letter1 and move to it
resetArms(); //move back to centre
}
void getUserInput(){
userInput = Serial.readString();
//Serial.println(userInput); //See if userInput is working
userInput.toCharArray(userInputArray,4);
//Serial.println(userInputArray); //See if userInputArray is working
letter1 = userInputArray[0];
letter2 = userInputArray[1];
letter3 = userInputArray[2];
Serial.println(letter1);
//Serial.print(letter2); // Check letters are working
//Serial.println(letter3);
}
void letter1Select(){
if (letter1 = "a"){
tapa();
}
else if (letter1 = "b"){
tapb();
}
else {
resetArms();
}
}
void tapa() {
for (posPan =90; posPan >=43; posPan -= 1){
pan.write(posPan);
delay(40);
}
// for (posTap =90; posTap >=50; posTap -= 1){
// tap.write(posTap);
// delay(40);
// }
// for (posTilt =90; posTilt <=114; posTilt += 1){
// tilt.write(posTilt);
// delay(40);
// }
}
void tapb() {
for (posPan =90; posPan <=180; posPan += 1){
pan.write(posPan);
delay(40);
}
// for (posTap =90; posTap >=40; posTap -= 1){
// tap.write(posTap);
// delay(40);
// }
// for (posTilt =90; posTilt >=60; posTilt -= 1){
// tilt.write(posTilt);
// delay(40);
// }
// for (posTap =40; posTap >=20; posTap -= 1){
// tap.write(posTap);
// delay(20);
// }
// for (posTap =20; posTap <=40; posTap += 1){
// tap.write(posTap);
// delay(40);
// }
}
void resetArms(){
pan.write(90);
tilt.write(90);
tap.write(90);
}
//}