Playful robots [part 2]
On the top of the robot, we add a platform to hold the breadboard and the attacking mechanism.
As for the attacking mechanism, we imitate the pokemon “Dodrio” so that it can cover more areas. Whenever the ultrasonic sensor detects other objects coming from either right, left, or forward, it will swing its hammer to smash others’ balloons. When designing the hammer, considering the capacity of the servo is limited and it cannot carry very heavy weapons. I use some long nails and a foam ball to make the shape of a mace. This is our drawing prototype of what we have in our minds.
Based on this physical structure, we draw a code flow to help us clarify in what situation the robot should do what thing.
The biggest issue we encounter in the fabrication process is the bad sensor. Somehow, several sensors and servos we borrow from the ER don’t work so we have to tear up the whole circuit to figure out what has happened.
After trying it several times, it works perfectly.
And this is our Arduino Code.
#include <Servo.h> #include <NewPing.h> Servo myservo1; // create servo object to control a servo Servo myservo2; Servo myservo3; #define TRIGGER_PIN_H 3 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN_H 4 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN_L 10 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN_L 9 #define TRIGGER_PIN_R 7 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN_R 8 #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. #define Head 0 #define Left 1 #define Right 2 #define Stop 3 #define Far 4 NewPing sonar_H(TRIGGER_PIN_H, ECHO_PIN_H, MAX_DISTANCE); // HeadPing setup of pins and maximum distance. NewPing sonar_L(TRIGGER_PIN_L, ECHO_PIN_L, MAX_DISTANCE); // LeftPing setup of pins and maximum distance. NewPing sonar_R(TRIGGER_PIN_R, ECHO_PIN_R, MAX_DISTANCE); // RightPing setup of pins and maximum distance. //IR sensor int IRSensor = 2; // connect ir sensor to arduino pin 2 int LED = 13; // conect Led to arduino pin 13 //Standard PWM DC control int E1 = 5; //M1 Speed Control int E2 = 6; //M2 Speed Control int M1 = 4; //M1 Direction Control int M2 = 7; //M1 Direction Control int distance_H; int distance_L; int distance_R; int statusSensor; int state; int val1; int val2; int val3; int pos = 0; void stop(void) //Stop { digitalWrite(E1, LOW); digitalWrite(E2, LOW); } void advance(char a, char b) //Move forward { analogWrite (E1, a); //PWM Speed Control digitalWrite(M1, HIGH); analogWrite (E2, b); digitalWrite(M2, HIGH); } void back_off (char a, char b) //Move backward { analogWrite (E1, a); digitalWrite(M1, LOW); analogWrite (E2, b); digitalWrite(M2, LOW); } void turn_L (char a, char b) //Turn Left { analogWrite (E1, a); digitalWrite(M1, LOW); analogWrite (E2, b); digitalWrite(M2, HIGH); } void turn_R (char a, char b) //Turn Right { analogWrite (E1, a); digitalWrite(M1, HIGH); analogWrite (E2, b); digitalWrite(M2, LOW); } void readSensors() { distance_H = sonar_H.ping_cm(); distance_L = sonar_L.ping_cm(); distance_R = sonar_R.ping_cm(); if (distance_H == 0) { distance_H = 200; } if (distance_L == 0) { distance_L = 200; } if (distance_R == 0) { distance_R = 200; } } void setup(void) { int i; for (i = 4; i <= 7; i++) pinMode(i, OUTPUT); Serial.begin(9600); //Set Baud Rate //IR_sensor pinMode (IRSensor, INPUT); // sensor pin INPUT pinMode (LED, OUTPUT); // Led pin OUTPUT //servo myservo1.attach(11); myservo2.attach(12); myservo3.attach(13); } void loop(void) { int statusSensor = digitalRead (IRSensor); readSensors(); if (statusSensor == 1) { digitalWrite(LED, LOW); turn_L(255, 255); delay(1100); } else { advance(255, 255); digitalWrite(LED, HIGH); Serial.print(distance_H); Serial.print(" "); Serial.print(distance_L); Serial.print(" "); Serial.print(distance_R); Serial.println(); if (distance_H <= 10) { for (pos = 90; pos <= 180; pos += 1) { myservo1.write(pos); delay(3); } for (pos = 180; pos >= 90; pos -= 1) { myservo1.write(pos); delay(3); } } else if (distance_L <= 10) { for (pos = 90; pos <= 180; pos += 1) { myservo2.write(pos); delay(3); } for (pos = 180; pos >= 90; pos -= 1) { myservo2.write(pos); delay(3); } } else if (distance_R <= 10) { Serial.print("right"); for (pos = 0; pos <= 90; pos += 1) { myservo3.write(pos); delay(3); } for (pos = 90; pos >= 0; pos -= 1) { myservo3.write(pos); delay(3); } } } }