Blog

Motion Sensor & Buzzer Code

IMG_4177

Circuit diagram of the Motion Sensor with Buzzer.

const int sensorPin = 10;
const int buzzerPin = 8;
const int toneID = 294;

void setup() {
// put your setup code here, to run once:
pinMode(sensorPin,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

int sensorVal = digitalRead(sensorPin);
if(sensorVal == HIGH){
tone(buzzerPin, toneID, 100);
}else{
noTone(buzzerPin);
}
delay(1000);
}

Advertisements

Autonomous RC Car Code

const int turnOnMotorPin = 11;
const int controlMotorPin_1 = 12;
const int controlMotorPin_2 = 10;
const int turnMotor_1 = 9;
const int turnMotor_2 = 8;
const int echoSensor = 7;
const int triggerSensor = 6;

int motion = 1; //forward
int direction = 0; // straight

void setup() {
pinMode(controlMotorPin_1, OUTPUT);
pinMode(controlMotorPin_2, OUTPUT);
pinMode(turnOnMotorPin, OUTPUT);
pinMode(turnMotor_1, OUTPUT);
pinMode(turnMotor_2, OUTPUT);
pinMode(echoSensor,INPUT);
pinMode(triggerSensor,OUTPUT);
digitalWrite(turnOnMotorPin, HIGH);

delay(5000); // Some initial delay to place the car in the right location

}

void loop() {

long duration, inches;
digitalWrite(triggerSensor, LOW);
delayMicroseconds(2);
digitalWrite(triggerSensor, HIGH);
delayMicroseconds(10);
digitalWrite(triggerSensor, LOW);
pinMode(echoSensor, INPUT);
duration = pulseIn(echoSensor, HIGH);
inches = microsecondsToInches(duration);

int oldMotion = motion;

if(motion == 1 && inches > 15){
forward();
motion = 1;
}else
if(motion == 1 && inches <= 15){
reverse();
motion = 0;
}else if(motion == 0 && inches 30){
forward();
motion = 1;
}else {
forward();
motion = 1;
}

if(oldMotion != motion){
direction = random(0, 2);
}

if(direction == 0){
goStragith();
}else if(direction == 1){
changeDirectionLeft();
}else {
changeDirectionRight();
}

delay(100);
}

void changeDirectionLeft(){

digitalWrite(turnMotor_1, HIGH);
digitalWrite(turnMotor_2, LOW);
}

void changeDirectionRight(){

digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, HIGH);
}
void goStragith(){

digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, LOW);
}

void stop(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, LOW);
}

void forward(){
digitalWrite(controlMotorPin_1, HIGH);
digitalWrite(controlMotorPin_2, LOW);
}

void reverse(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, HIGH);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

Temperature Sensor Code

The following is the code for using the Temperature Sensor:

const int tempSensorOutputPin = A0;

const int firstLEDPwrPin = 4;
const int secondLEDPwrPin = 3;
const int thirdLEDPwrPin = 2;

int tempSensorValue = 0;

void setup() {
Serial.begin(9600);
for(int pinLoop = 2; pinLoop <=4; pinLoop++){
pinMode(pinLoop, OUTPUT);
digitalWrite(pinLoop,LOW);
}
}

void loop() {
run();
}

void run(){

tempSensorValue = analogRead(tempSensorOutputPin);
float voltage = (tempSensorValue / 1024.0) * 5.0;
float temp = (voltage - 0.5) * 100;
Serial.print("Temp = ");
Serial.println(temp);
delay(1000);

}

Proximity Sensor Code

const int greenLightPin = 6;
const int yellowLightPin = 5;
const int redLightPin = 4;

const int ussTrigPin = 2;
const int ussEchoPin = 3;

void setup() {
// put your setup code here, to run once:
pinMode(greenLightPin,OUTPUT);
pinMode(yellowLightPin,OUTPUT);
pinMode(redLightPin,OUTPUT);

pinMode(ussTrigPin,OUTPUT);
pinMode(ussEchoPin,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
long duration, inches;
digitalWrite(ussTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ussTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ussTrigPin, LOW);
pinMode(ussEchoPin, INPUT);
duration = pulseIn(ussEchoPin, HIGH);
inches = microsecondsToInches(duration);
if(inches > 10 && inches < 15){
digitalWrite(greenLightPin, HIGH);
delay(100);
digitalWrite(greenLightPin, LOW);
delay(100);
}
if(inches 5){
digitalWrite(yellowLightPin, HIGH);
delay(100);
digitalWrite(yellowLightPin, LOW);
delay(100);
}
if(inches < 5){
digitalWrite(redLightPin, HIGH);
delay(100);
digitalWrite(redLightPin, LOW);
delay(100);
}

}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}