This is an old revision of the document!
Author : Ghibea Ovidiu-Catalin
Group : 1221A
Faculty : FILS
This robot has an anxiety problem and does not want to meet with anyoane or anything.
Small robot that is capable of sensing incoming obstacles and avoid them. Once the switch is turned on, the robot will start moving in a straight line. The ultrasonic sensor placed on the front of the robot will pick up any incoming waves and will translate this into distance in the code. If the received distance will be lower than a certain threshold He will stop and signal to the motor driver module to turn left until there is nothing in front of him anymore, after which he will continue moving.
Components
Name | Quantity |
---|---|
Arduino Uno | 1 |
HC-SR04 Ultrasonic Sensor | 1 |
Speaker | 1 |
LM298N Motor Driver Module | 1 |
5V DC Motors | 2 |
Switch | 1 |
9V Battery | 1 |
Wheels | 2 |
Chassis | 1 |
Jumper Wires | 1 |
Electric diagram:
We begin by declaring all global variables and creating the classes needed for the player.
#define BUTTON 2//detonator button #define LEDRED 5//armed LED #define LEDLEFT 6 // yellow LED #define LEDMIDDLE 7 // yello LED #define LEDRIGHT 8 //yellow LED #define SENSOR 9 //sensor input #define TX 10 //TX pin on DFPlayer module #define RX 11 //RX pin on DFPlayer module const int time = 2124;//the delay between sequence change int sequenceNumber = 0; SoftwareSerial softwareSerial(TX, RX); DFRobotDFPlayerMini player; // declaration of player
We create two functions: one that will turn on the yellow LEDs in a given configuration (i. e. left-on, middle-off, right-on) and a function that returns the value of the movement sensor.
//Run one LED sequence void Sequence(bool Left, bool Middle, bool Right){ if (digitalRead(BUTTON) == LOW)//if true (button is pressed) run LED sequence { if (Left) { digitalWrite(LEDLEFT, HIGH); } else { digitalWrite(LEDLEFT, LOW); } if (Middle) { digitalWrite(LEDMIDDLE, HIGH); } else { digitalWrite(LEDMIDDLE, LOW); } if (Right) { digitalWrite(LEDRIGHT, HIGH); } else { digitalWrite(LEDRIGHT, LOW); } } } long TP_init(){ long measurement=pulseIn (SENSOR, HIGH); //wait for the pin to get HIGH and returns measurement return measurement; }
After this, we begin the setup process by starting the program and setting all LEDs on LOW.
void setup() { Serial.begin(9600); pinMode(SENSOR, INPUT); softwareSerial.begin(9600); pinMode(LEDRED, OUTPUT); pinMode(LEDLEFT, OUTPUT); pinMode(LEDMIDDLE, OUTPUT); pinMode(LEDRIGHT, OUTPUT); digitalWrite(LEDRED, LOW); digitalWrite(LEDLEFT, LOW); digitalWrite(LEDMIDDLE, LOW); digitalWrite(LEDRIGHT, LOW); //now set pins to input pinMode(BUTTON, INPUT_PULLUP); // start the player player.begin(softwareSerial); }
The loop() part of the program is made out of two scenarios.
In the first scenario, the switch is off (The convention for this project is that if the microswitch is off, the circuit is active. If the switch is on, then the circuit is off. The reason for this being the outer design of the toy as well as the desire to make it movie accurate) and the program begins the arming sequence. It receives the measurement from the movement sensor and if the measurement is smaller than the chosen value, it keeps on with the arming sequence. If the measurement is greater than the chosen value, it stops the previous sequence by stopping track 1 and starts the detonation sequence by playing track 2 and turning all LED’s on HIGH.
// Start communication with DFPlayer Mini // Set volume to a value between (0 to 30) player.volume(30); if (digitalRead(BUTTON) == LOW ){ player.play(1); // play the arming sequence digitalWrite(LEDRED, HIGH);//switch armed LED (red) on } if (digitalRead(BUTTON) == LOW && sequenceNumber == 0) { long measurement =pulseIn (SENSOR, HIGH); //get measurement from teh sensor Serial.println(measurement); if (measurement > 1000) { // measurement checks player.stop(); player.play(2); //playing detonating sequence sequenceNumber = 0; digitalWrite(LEDRED, HIGH); digitalWrite(LEDRIGHT, HIGH); digitalWrite(LEDMIDDLE, HIGH); digitalWrite(LEDLEFT, HIGH); while(digitalRead(BUTTON)==LOW){ // give a chance to turn off the toy delay(10000); player.stop(); digitalWrite(LEDRED, LOW); digitalWrite(LEDRIGHT, LOW); digitalWrite(LEDMIDDLE, LOW); digitalWrite(LEDLEFT, LOW); } } else { // measuremnt does not check delay(time); Sequence(false, true, true); // continu with arming sequence sequenceNumber++; } }
The second scenario is when the switch is on, so the circuit is not active.
if (digitalRead(BUTTON) == HIGH) // toy is turned off { sequenceNumber = 0; player.stop(); digitalWrite(LEDRED, LOW); digitalWrite(LEDRIGHT, LOW); digitalWrite(LEDMIDDLE, LOW); digitalWrite(LEDLEFT, LOW); }
Additionally, depending on the length of track 1 used in the arming sequence, we will choose a number of times the respective if{} statement will be repeated. In this case it, is repeated 12 times.
Fişierele se încarcă pe wiki folosind facilitatea Add Images or other files. Namespace-ul în care se încarcă fişierele este de tipul :pm:prj20??:c? sau :pm:prj20??:c?:nume_student (dacă este cazul). Exemplu: Dumitru Alin, 331CC → :pm:prj2009:cc:dumitru_alin.