So today, I started a project with my new Arduino Nano and some 3D printed parts. Basically, it's a small robot that will go forward until two buttons at the front are pressed. For example, if the robot crashed into a wall, the two buttons at the front would detect that, and the robot would stop for 10 seconds.
Currently, I have a "drive" function that will drive the robot, and I'm also trying to make a detect function to detect if the buttons are pressed.
Here's my function code:
int IN1 = 3;
int IN2 = 9;
int IN3 = 10;
int IN4 = 11;
int b1Pin = 8;
int b2Pin = 7;
void drive(int lSpeed, int rSpeed){
if(lSpeed>0){
analogWrite(IN1, lSpeed);
digitalWrite(IN2, 0);
}
else{
digitalWrite(IN1, 0);
analogWrite(IN2, -lSpeed);
}
if(rSpeed>0){
analogWrite(IN3, rSpeed);
digitalWrite(IN4, 0);
}
else{
digitalWrite(IN3, 0);
analogWrite(IN4, -rSpeed);
}
}
void detectCrash() {
if(digitalRead(b1Pin)){
delay(10000);
}
if(digitalRead(b2Pin)){
delay(10000);
}
}
void setup() {
// put your setup code here, to run once:
drive(255,255);
detectCrash();
}
void loop() {
// put your main code here, to run repeatedly:
}
Sadly, this code doesn't work. Does anyone know some (ideally easy-to-understand) code that can achieve my goal?
Thank you!
EDIT: The code is written in Arduino C++