Hey, I have made this two-wheeled garbage collector bot with a servo at the front working as an excavator. I have coded it, and the servo works. I have used the touchdown and touch-up functions for the movement so that when I press the button works and on release, it stops working but it doesn't work that way, in fact upon clicking at times it starts working on its own or it chooses not to work until I have to spam the button to make it stop. PLEASE HELP ME
Arduino IDE code:
#include <Servo.h>
Servo myservo;
int m1a = 9;
int m1b = 10;
int m2a = 11;
int m2b = 12;
char val;
void setup()
{
myservo.attach(13);
Serial.begin(9600);
pinMode(m1a, OUTPUT); // Digital pin 10 set as output Pin
pinMode(m1b, OUTPUT); // Digital pin 11 set as output Pin
pinMode(m2a, OUTPUT); // Digital pin 12 set as output Pin
pinMode(m2b, OUTPUT); // Digital pin 13 set as output Pin
}
void loop()
{
if(Serial.available()>0)
{
char data = Serial.read();
if(data=='a')
{
myservo.write(0);
}
if(data=='b')
{
myservo.write(30);
}
if(data=='c')
{
myservo.write(45);
}
if(data=='d')
{
myservo.write(60);
}
if(data=='e')
{
myservo.write(80);
}
}
{
while (Serial.available() > 0)
{
val = Serial.read();
Serial.println(val);
}
if( val == 'F') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if(val == 'B') // Backward
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}
else if(val == 'L') //Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if(val == 'R') //Right
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == 'S') //Stop
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
}
}