CrEp4E the object avoidance buzzing & blinking theremin robot :). With Update ! Bump Switches are functional

Hello to all, this is my first lmr added project, i've build robot in arduino ide, it's verry simple code, i've made this robot from scrap as you can see i found my old dvd's and i wanted to throw them away in the garbage, so i thought my self what can i do with them if i recycle them so i wanted to build a cd/dvd coaster bot i found some lds, my arduino board and some resistors and some secretary machine box and i took from there some bump switches and i got some plastic laying around so i wen't to near electronics shop and bought blank pcb and l293d motor controller, and terminal blocks, so i made a pcb layout in circuit wizard pc program and asked my dad for some rechargable bateries, he said that we've had in our weekend village house, and i found a 2AA battery pack with litle switch and than i bought a 9V battery to power the arduino board. After 3 weeks i've build my first microcontroller based robot. it was navigating with bump switches. i almost forgot i've got the motors for the robot from a rc car which i bought from my near mall about 600 denars i bought 2 of these rc cars. After all i was doing this in the end of the 3th high school year, and i didn't have much time to make more esthetic, So when my school year ended i wanted to rebuild and to add ultrasonic sensor for better navigation and some leds and a speaker, so i've done that. The robot works, but i want to make the bump switches functional with the ultrasonic sensor together. So if some one can help me about this problem it would be very great and i'll be very thankful. so here is the video of my first robot with bump switches, link: http://www.youtube.com/watch?v=VQovABdRbh4 down in the description is the code for the robot and pcb layout for the motor controller board that i'am using on the both robots the new and the old one. The codes are simple but i don't know how to combine them :). And Big THANKS TO HACKLAB KIKA THE FIRST HACKER SPACE IN MACEDONIA for borowing the srf05 ultrasonic distance sensor.

Here are some images 

final update od the code: http://pastebin.com/Ckya95be

 

It acts as a theremin instrument and avoids obstacles also entertain :)

  • Actuators / output devices: toy car steering geared motors, L293D Motor controller
  • Control method: Autonomous.
  • CPU: Arduino Duemilanove
  • Operating system: Arduino bootloader, Windows XP and Lubuntu
  • Power source: 9v battery, 4AA Rechargable battery pack
  • Programming language: C/C++
  • Sensors / input devices: bumper switches, SRF05Ultra Sound
  • Target environment: Smooth surfaces., Indor

This is a companion discussion topic for the original entry at https://community.robotshop.com/robots/show/crep4e-the-object-avoidance-buzzing-blinking-theremin-robot-with-update-bump-switches-are-functional

Thank you :slight_smile:

Thank you :slight_smile:

Just a thought

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const byte numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value

// setup pins and variables for SRF05 sonar device

const byte echoPin = 2; // SRF05 echo pin (digital 2)
const byte initPin = 3; // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
unsigned long soundDelay = 0;
const byte speakerPin = 5;
const byte motor1Pin1 = 8; // pin 2 on L293D
const byte motor1Pin2 = 7; // pin 7 on L293D
const byte motor2Pin1 = 10; // pin 10 on L293D
const byte motor2Pin2 = 9; // pin 15 on L293D
const byte led1 = 12;
const byte led2 = 4;
const byte leftSwitch = 6;
const byte rightSwitch = 11;
//const byte ENABLE1 = ; // uncomment and include a pin to connect the ENABLE to
//const byte ENABLE2 = ; // uncomment and include a pin to connect the ENABLE to
boolean readingLeftSwitch = LOW; // the current reading from the left input pin
boolean readingRightSwitch= LOW; // the current reading from the right input pin

void setup() {
// set the motor pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
//pinMode(ENABLE1, OUTPUT);
//pinMode(ENABLE2, OUTPUT);
pinMode(initPin, OUTPUT); // set init pin 3 as output
pinMode(echoPin, INPUT); // set echo pin 2 as input
pinMode(speakerPin, OUTPUT); // sets pin 6 as outpu
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

// create array loop to iterate over every item in the array
//not really required. Arrays are initialized to 0 in all elements
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
}

void loop() {
for (int i = 0; i < numOfReadings; i++) {
distance = distanceCheck();
total += distance;
}
readingLeftSwitch = digitalRead(leftSwitch);
readingRightSwitch = digitalRead(rightSwitch);
//~ total = total - readings[arrayIndex]; // subtract the last distance
//~ readings[arrayIndex] = distance; // add distance reading to array
//~ total = total + readings[arrayIndex]; // add the reading to the total
//~ arrayIndex = arrayIndex + 1; // go to the next item in the array

//~ // At the end of the array (10 items) then start again
//~ if (arrayIndex >= numOfReadings) {
//~ arrayIndex = 0;
//~ }

averageDistance = total / numOfReadings; // calculate the average distance
delay(10);

// check the average distance and move accordingly
if ((averageDistance <= 10) || ((readingLeftSwitch == HIGH) &&
(readingRightSwitch == HIGH))) {
// go backwards
move(-100, -100);
}
if ((averageDistance <= 25 && averageDistance > 10) || ((readingLeftSwitch == LOW) &&
(readingRightSwitch == HIGH))) {
// turn
move(100, -100);
}
if ((readingLeftSwitch == HIGH) && (readingRightSwitch == LOW)) {
// turn
move(-100, 100);
}
if ((averageDistance > 25) || ((readingLeftSwitch == LOW) &&
(readingRightSwitch == LOW))) {
// go forward
move(100, 100);
}
}

void move(int left, int right) {
//ints are passed so when you add PWM to the ENABLE pins you can control
//the speed of each motor
if (left > 0) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(led1, LOW);
} else if (left < 0) {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(led1, HIGH);
} else {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(led1, LOW);
}
if (right > 0) {
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(led2, LOW);
} else if (right < 0) {
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(led2, HIGH);
} else {
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(led2, LOW);
}
//analogWrite(ENABLE1, left);
//analogWrite(ENABLE2, right);
}

int distanceCheck() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
// Look for a return pulse, it should be high as the pulse goes low-high-low
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
soundDelay = pulseTime/3;
if (distance < 25) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(soundDelay);
digitalWrite(speakerPin, LOW);
delayMicroseconds(soundDelay);
}
return distance;
}

All I can say about the code above is that it compiles. It looks right to me, but, I make no promises.