Hello,
I'm currently working on my first project, the SMARS Robot Car by Kevin Thomas. I didn't quite follow the instructions and used different components, so I'll describe it briefly in bullet points.
- Arduino Uno Rev. 3
- L293d Motor Shield on the Arduino Uno
- 2x N20 mini geared motor
- Ultrasonic sensor hc-sr04
- Infrared sensor
- JOY-IT Wireless Gamepad
- Power supply with Amazon Basics battery
Battery (button switch to turn on and off) - Passive buzzer (without resistance)
Everything works fine except for the passive buzzer. Whenever I press the buzzer (in my case with "SELECT"), nothing works. Neither the manual control works afterwards, nor the other programmed functions.
Unfortunately, I don't know whether there is an error in the code or whether it is the hardware. So I'll post my code and get back to you if I have any further questions. I would also like to add that I am an absolute beginner and apologize for individual errors in the forum. All my efforts have not helped and I could not find a similar topic to my problem.
I want the other buttons to work after I use the buzzer. Actually, I also intended to emit a sound when I press a button. But unfortunately my code seems to hang up.
#include <PS2X_lib.h>
#include <AFMotor.h>
#include <pitches.h>
#define PS2_DAT 9 // data
#define PS2_CMD 10 // command
#define PS2_SEL 2 // attention
#define PS2_CLK 13 // clock
PS2X ps2x; // create PS2 Controller Class
int error = 0; // for controller-connenction
byte type = 0; // for controller-type
AF_DCMotor lmotor(1); // left motor
AF_DCMotor rmotor(2); // right motor
int echo = A1; // echo pin connected to A1
int trigger = A2; // trig pin connected to A2
long duration; // variable for ultrasonic-sensor
int theDistance = 0; // variable for ultrasonic-sensor
int keepDistance = 8; // variable for ultrasonic-sensor
int infrared = A0; // variable for infrared-sensor
int bobber = A5; // variable for buzzer
int melody[] = {
NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_C4, NOTE_F4,
NOTE_E4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_C4, NOTE_G4,
NOTE_F4, NOTE_C4, NOTE_C4,
NOTE_C5, NOTE_A4, NOTE_F4,
NOTE_E4, NOTE_D4, NOTE_AS4, NOTE_AS4,
NOTE_A4, NOTE_F4, NOTE_G4,
NOTE_F4
};
int durations[] = {
4, 8,
4, 4, 4,
2, 4, 8,
4, 4, 4,
2, 4, 8,
4, 4, 4,
4, 4, 4, 8,
4, 4, 4,
2
};
//-------------------------------------------------- SETUP
void setup() {
Serial.begin(9600);
delay(400); // time to startup, before configuring it
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT); // setup pins for PS2 Controller
if (error == 0) { // check for errors
Serial.print("PS2 Controller successfully found and configured, B.O.B. is waiting :) -> ");
} else {
Serial.print("No PS2 Controller found, try again ");
setup();
}
type = ps2x.readType(); // read type of Controller
switch (type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
}
lmotor.setSpeed(200); // left motor speed 200
rmotor.setSpeed(200); // right motor speed 200
lmotor.run(RELEASE); // release left motor
rmotor.run(RELEASE); // release right motor
pinMode(echo, INPUT); // echo as input
pinMode(trigger, OUTPUT); // trigger as output
pinMode(infrared, INPUT); // infrared as input
pinMode(bobber, OUTPUT); // buzzer as output
}
//-------------------------------------------------- LOOP
void loop() {
if (error == !0) // skip loop if no Controller found
return;
if (type == 1) { // DualShock Controller
ps2x.read_gamepad(); // read Controller
manualcontrol(); // use d-pad for manual control (already active)
if (ps2x.Button(PSB_TRIANGLE)) // hold triangle to activate obstacle avoidance
obstacleavoidance();
if (ps2x.Button(PSB_CIRCLE)) // hold circle to activate following BOB
followingbob();
if (ps2x.Button(PSB_CROSS)) // hold cross to activate black-line driving
blacklinedriving();
if (ps2x.Button(PSB_SQUARE)) // hold square to activate avoid the black
avoidtheblack();
if (ps2x.ButtonPressed(PSB_START)) // press start to activate bobby-dance
bobbydance();
if (ps2x.ButtonPressed(PSB_SELECT)) // press select to activate bobbybirthday
bobbybirthday();
}
delay(50);
}
//-------------------------------------------------- MANUAL CONTROL
void manualcontrol() {
if (ps2x.Button(PSB_PAD_UP)) { // move forward
lmotor.run(FORWARD);
rmotor.run(FORWARD);
} else if (ps2x.Button(PSB_PAD_DOWN)) { // move backward
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
} else if (ps2x.Button(PSB_PAD_LEFT)) { // turn left
lmotor.run(BACKWARD);
rmotor.run(FORWARD);
} else if (ps2x.Button(PSB_PAD_RIGHT)) { // turn right
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
} else { // stop
lmotor.run(RELEASE);
rmotor.run(RELEASE);
}
}
//-------------------------------------------------- OBSTACLE AVIODANCE
void obstacleavoidance() {
theDistance = distance(); // store the measured distance
Serial.println(theDistance); // print the measured distance
if (theDistance <= 12) { // move backward and turn right if distance is less than 12cm
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
delay(500);
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
delay(500);
} else { // otherwise move forward
lmotor.run(FORWARD);
rmotor.run(FORWARD);
}
}
//-------------------------------------------------- FOLLOWING BOB
void followingbob() {
theDistance = distance(); // store the measured distance
Serial.println(theDistance); // print the measured distance
if (theDistance < keepDistance && theDistance >= 0) { // move backward if the distance is less than 8cm
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
}
if (theDistance > (keepDistance + 3) && theDistance < (keepDistance + 20)) { // move forward if the distance is more than 11cm
lmotor.run(FORWARD);
rmotor.run(FORWARD);
}
if (theDistance > (keepDistance + 20)) { // turn right if the distance is more than 28cm
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
}
if (theDistance >= keepDistance && theDistance <= (keepDistance + 3)) { //stop if the distance is between 8cm and 11cm
lmotor.run(RELEASE);
rmotor.run(RELEASE);
}
}
//-------------------------------------------------- IN ADDITION FOR OBSTACLE AVOIDANCE AND FOLLOWING BOB
int distance() {
int echoTime; // store the time for a ping to bounce off an object
int calculatedDistance; // store the calculated distance from the echo time
digitalWrite(trigger, HIGH); // ultrasonic pulse for 10ms
delayMicroseconds(10);
digitalWrite(trigger, LOW);
echoTime = pulseIn(echo, HIGH); // time for the pulse to bounce back to the sensor
calculatedDistance = echoTime / 2 / 29; // calculate the distance of the reflected object (half the bounce time multiplied by the speed of sound)
return calculatedDistance; // send back the calculated distance
}
//-------------------------------------------------- BLACK-LINE DRIVING
void blacklinedriving() {
int ifrstate = digitalRead(infrared); // store the state of the infrared-sensor
if (ifrstate == 1) { // move forward if black line detected
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
} else { // turn right if not on a black line
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
}
}
//-------------------------------------------------- AVOID THE BLACK
void avoidtheblack() {
int ifrstate = digitalRead(infrared); // store the state of the infrared-sensor
if (ifrstate == 1) { // move backward and turn right if black line detected
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(500);
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
delay(500);
} else { // move forward if not on a black line
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
}
}
//-------------------------------------------------- BOBBY-DANCE
void bobbydance() {
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(1000);
lmotor.run(RELEASE);
rmotor.run(RELEASE);
delay(1000);
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(200);
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
delay(200);
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(200);
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
delay(200);
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(200);
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
delay(200);
lmotor.run(FORWARD);
rmotor.run(FORWARD);
delay(200);
lmotor.run(BACKWARD);
rmotor.run(BACKWARD);
delay(1200);
lmotor.run(RELEASE);
rmotor.run(RELEASE);
delay(200);
lmotor.run(BACKWARD);
rmotor.run(FORWARD);
delay(2000);
lmotor.run(FORWARD);
rmotor.run(BACKWARD);
delay(2000);
}
void bobbybirthday() {
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++) {
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(bobber, melody[note], duration);
//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing:
noTone(bobber);
}
}
Dominik Bubak