-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a67be95
commit f3a654e
Showing
4 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
ROBOTHON ELECTRONIX ENSTAB | ||
Workshop 1 : IR Sensor | ||
Copy and Paste this code on your Arduino IDE | ||
This code is delicately typed by the ELECTRONIX Club for whom this may be of concern | ||
HEDFI Wassim. | ||
*/ | ||
|
||
|
||
#define sensor 9 // Call the sensor's output pin(9) as sensor, this will help when you need to change the pin, for any reason | ||
// if you want to change the pin number you want have to do so for all the code (wherever you used the pin 9), but just change it here | ||
// you want to call the sensor pin in the code ? just type sensor ! | ||
int sensorValue ; // Create an empty variable to store the sonsor's reading | ||
|
||
void setup() { | ||
|
||
pinMode(sensor,INPUT); // Set the pin 'sensor'(9) to INPUT Mode; our Arduino will receive data from the sensor, hence this pin should be input | ||
Serial.begin(9600); // This will set the our Arduino to cmmunicate with the Serial Monitor of the Arduino IDE with a bandwidth of 9600 | ||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
sensorValue=digitalRead(sensor); // the function "digitalRead" reads the value on the pin 'sensor'(9) and return a value of 0 or 1 | ||
Serial.println(sensorValue); // The function 'Serial.println' will write the value on the Serial Monitor | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
ROBOTHON ELECTRONIX ENSTAB | ||
Workshop 1 : IR Sensor | ||
Copy and Paste this code on your Arduino IDE | ||
This code is delicately typed by the ELECTRONIX Club for whom this may be of concern | ||
HEDFI Wassim. | ||
*/ | ||
|
||
|
||
#include <SoftwareSerial.h> | ||
|
||
#define DEBUG_ENABLE // Comment this line before putting the code in the robot to remove serial printing | ||
// otherwise may slow down the performance of your robot | ||
|
||
#ifdef DEBUG_ENABLE | ||
#define DEBUG_MACRO(STRING) Serial.println(STRING) | ||
#else | ||
#define DEBUG_MACRO(STRING) // DO Nothing | ||
#endif | ||
|
||
|
||
#define ENABLE_1_PIN 10 // same as we did in the previous workshop (3) | ||
#define MOTOR_1_INPUT_1 9 | ||
#define MOTOR_1_INPUT_2 8 | ||
#define MOTOR_2_INPUT_1 7 | ||
#define MOTOR_2_INPUT_2 6 | ||
#define ENABLE_2_PIN 5 | ||
|
||
#define SERIAL_RX_PIN 0 // Connected to the Blutooth module's TX | ||
#define SERIAL_TX_PIN 1 // Connected to the Blutooth module's RX ; don't forget the voltage divider ! | ||
|
||
|
||
|
||
#define MAX_MOTOR_SPEED 255 | ||
#define NORMAL_MOTOR_SPEED 150 | ||
#define SLOW_MOTOR_SPEED 80 | ||
|
||
// Software serial instance | ||
SoftwareSerial bluetoothSerial(SERIAL_RX_PIN, SERIAL_TX_PIN); // RX_PIN, TX_PIN | ||
char incomingData = 'r'; | ||
|
||
/* Functions prototypes */ | ||
void forward(uint8_t speed); | ||
void reverse(uint8_t speed); | ||
void right(uint8_t speed); | ||
void left(uint8_t speed); | ||
void sharpRightTurn(uint8_t speed); | ||
void sharpLeftTurn(uint8_t speed); | ||
void stopBot(uint8_t speed); | ||
void scanBluetooth(); | ||
|
||
/* ---------------------------------------------------------- */ | ||
void setup() { | ||
#ifdef DEBUG_ENABLE | ||
Serial.begin(9600); | ||
#endif | ||
|
||
bluetoothSerial.begin(9600); // This is the serial port we'll communicate with the bluetooth module through | ||
DEBUG_MACRO("--- Bluetooth RC robot started ---"); | ||
|
||
pinMode(MOTOR_1_INPUT_1, OUTPUT); | ||
pinMode(MOTOR_1_INPUT_2, OUTPUT); | ||
pinMode(MOTOR_2_INPUT_1, OUTPUT); | ||
pinMode(MOTOR_2_INPUT_2, OUTPUT); | ||
pinMode(ENABLE_1_PIN, OUTPUT); | ||
pinMode(ENABLE_2_PIN, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
scanBluetooth(); // Check if there's any incoming data through bluetooth | ||
|
||
switch(incomingData){ // Choose appropriate action | ||
case 'S': | ||
stopBot(NORMAL_MOTOR_SPEED); | ||
DEBUG_MACRO("STOPPING"); | ||
break; | ||
|
||
case 'F': | ||
forward(NORMAL_MOTOR_SPEED); | ||
DEBUG_MACRO("FORWARD"); | ||
break; | ||
|
||
case 'L': | ||
sharpLeftTurn(NORMAL_MOTOR_SPEED); | ||
DEBUG_MACRO("LEFT"); | ||
break; | ||
|
||
case 'R': | ||
sharpRightTurn(NORMAL_MOTOR_SPEED); | ||
DEBUG_MACRO("RIGHT"); | ||
break; | ||
|
||
case 'B': | ||
reverse(NORMAL_MOTOR_SPEED); | ||
DEBUG_MACRO("REVERSE"); | ||
break; | ||
|
||
default: | ||
DEBUG_MACRO("UNKNOWN COMMAND"); | ||
break; | ||
} | ||
/* | ||
forward(100); | ||
delay(1500); | ||
sharpRightTurn(100); | ||
delay(1500); | ||
sharpLeftTurn(100); | ||
delay(1500); | ||
reverse(100); | ||
delay(1500);*/ | ||
} | ||
/* ---------------------------------------------------------- */ | ||
|
||
void forward(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
|
||
void reverse(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void right(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void left(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
|
||
void sharpRightTurn(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void sharpLeftTurn(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
|
||
void stopBot(uint8_t speed){ | ||
/* The pin numbers and high, low values might be different depending on your connections */ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void scanBluetooth(){ | ||
// There's data in the serial buffer (available() returns the number of bytes available) | ||
if(bluetoothSerial.available() > 0) incomingData = bluetoothSerial.read(); // Read a byte | ||
else Serial.println(incomingData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
ROBOTHON ELECTRONIX ENSTAB | ||
Workshop 2 : Ultrasonic Sensor | ||
Copy and Paste this code on your Arduino IDE | ||
This code is delicately typed by the ELECTRONIX Club for whom this may be of concern | ||
HEDFI Wassim. | ||
*/ | ||
|
||
|
||
#define echoPin A3 // attacher echo du capteur ultrason a la pin A3 de la carte | ||
#define trigPin A2 // attacher trig du capteur ultrason a la pin A2 de la carte | ||
|
||
void setup() { | ||
// put your setup code here, to run once: | ||
pinMode(echoPin, INPUT); // echopin is an input for the Arduino board | ||
pinMode(trigPin,OUTPUT); | ||
Serial.begin(9600); // you saw this before ;) | ||
|
||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
//***calcul de distance à l'aide du capteur ultrason | ||
|
||
digitalWrite(trigPin, LOW); // initialize the trigger at 0 (LOW) | ||
delayMicroseconds(2); // keep it at zero for 2ms | ||
digitalWrite(trigPin, HIGH); // send a pulse (HIGH) on the trigger | ||
delayMicroseconds(10); // send the high pulse for 10ms | ||
digitalWrite(trigPin, LOW); // put it back at zero | ||
|
||
float duration = pulseIn(echoPin, HIGH); // the function 'pulsin' acts as a chronometer | ||
// when you call it it will start counting time and stop when echoPin is high and returns the duration | ||
// Calculating the distance | ||
float distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (from the sensor to the object and back == double the distance we want to calculate) | ||
float distanceCentimeter; // create a variable to store the distance | ||
if (distance>200) distanceCentimeter = 200; // ultrasonic sensor will give faulty measures for distances over 200cm, so the max distance we will define is 200cm | ||
else distanceCentimeter= distance; | ||
|
||
Serial.println(distanceCentimeter); // print the distance on the Seiral monitor | ||
delay(500); // make a little delay to not saturate the monitor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
ROBOTHON ELECTRONIX ENSTAB | ||
Workshop 2 : Ultrasonic Sensor | ||
Copy and Paste this code on your Arduino IDE | ||
This code is delicately typed by the ELECTRONIX Club for whom this may be of concern | ||
HEDFI Wassim. | ||
*/ | ||
|
||
|
||
|
||
#define ENABLE_1_PIN 10 // This should be a pwm (~) pin | ||
#define MOTOR_1_INPUT_1 9 | ||
#define MOTOR_1_INPUT_2 8 | ||
#define MOTOR_2_INPUT_1 7 | ||
#define MOTOR_2_INPUT_2 6 | ||
#define ENABLE_2_PIN 5 // This should be a pwm (~) pin | ||
|
||
|
||
|
||
|
||
#define MAX_MOTOR_SPEED 200 | ||
#define SLOW_MOTOR_SPEED 80 | ||
#define NORMAL_MOTOR_SPEED 100 | ||
|
||
|
||
|
||
void setup() { | ||
pinMode(MOTOR_1_INPUT_1, OUTPUT); // DEFINE | ||
pinMode(MOTOR_1_INPUT_2, OUTPUT); // MOTOR | ||
pinMode(MOTOR_2_INPUT_1, OUTPUT); // DRIVER | ||
pinMode(MOTOR_2_INPUT_2, OUTPUT); // PINS | ||
pinMode(ENABLE_1_PIN, OUTPUT); // AS | ||
pinMode(ENABLE_2_PIN, OUTPUT); // OUTPUT | ||
} | ||
|
||
void forward(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
|
||
void reverse(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void sharpleft(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
void left(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, HIGH); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
void sharpright(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, HIGH); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
void right(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, HIGH); | ||
} | ||
void stopBot(uint8_t speed){ | ||
analogWrite(ENABLE_1_PIN, speed); //Left Motor Speed | ||
analogWrite(ENABLE_2_PIN, speed); //Right Motor Speed | ||
digitalWrite(MOTOR_1_INPUT_1, LOW); | ||
digitalWrite(MOTOR_1_INPUT_2, LOW); | ||
digitalWrite(MOTOR_2_INPUT_1, LOW); | ||
digitalWrite(MOTOR_2_INPUT_2, LOW); | ||
} | ||
|
||
|
||
|
||
void loop() { | ||
forward(NORMAL_MOTOR_SPEED); | ||
delay(2000); | ||
sharpright(NORMAL_MOTOR_SPEED); | ||
delay(2000); | ||
sharpleft(NORMAL_MOTOR_SPEED); | ||
delay(2000); | ||
reverse(NORMAL_MOTOR_SPEED); | ||
delay(2000); | ||
} |