The assignment idea was Design and program a smart device that perform a certain function or solve a problem: program Arduino UNO to read signals from multiple input components (Sensor, Switch, or variable resistor) to control multiple action components (Motor, Buzzer, LED...etc) using Arduino C (Text Code). and I coudn't find somthing suitable on Instructables and Arduino Project Hub, So I picked (Ultarasonic, Pushbutton, buzzer and leds ) randomly to design the smart device.
I used tinkercad to design the circuit, then I moved to Arduino Idm to wite down the code and next step was returning to tinkercad to simulate the code and the circuit together and fix the errors
I designed the circuit first on tinkercad
the code was written on Arduino IDM
simulation on thinkercad for both curcit and code
conncet the wires on real
RUN the code
Materials:
cardboard
Arduino uno
ultrasonic
pushbutton
leds
Jumpers
breadboard
resistors
the whole implementation process was in the code, So
I started the code with the ultrasonic defination , Which I searched about it:
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
Then I defined the inputs and the outputs :
pinMode(7, OUTPUT);
pinMode(6, INPUT);
pinMode(2, INPUT_PULLUP);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
The last paragraph was about the conditions I wrote based on distances I need:
if (0.01723 * readUltrasonicDistance(7, 6) > 50 && digitalRead(2) < HIGH) {
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
noTone(3);
} else {
if (0.01723 * readUltrasonicDistance(7, 6) < 50 && (0.01723 * readUltrasonicDistance(7, 6) > 30 && digitalRead(2) < HIGH)) {
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(9, LOW);
noTone(3);
} else {
if (0.01723 * readUltrasonicDistance(7, 6) < 30 && (0.01723 * readUltrasonicDistance(7, 6) > 5 && digitalRead(2) < HIGH)) {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
noTone(3);
} else {
if (0.01723 * readUltrasonicDistance(7, 6) < 5 && (0.01723 * readUltrasonicDistance(7, 6) > 0 && digitalRead(2) < HIGH)) {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(9, LOW);
tone(3, 523, 1000); // play tone 60
}
}
}
}
delay(10);
The conditions are:
switch on and distance more than 50 -> green led is on
switch on and distance less than 50 and more than 30 -> yellow led is on
switch on and distance less than 30and more than 5-> red led is on
switch on and distance less than 5 and more than 0-> buzzer is on
I didn't ask for the feedback. I didn't help anyone and noone helped me.
I worked on tinkercad till it works like expected, but when I connected thed wires on real, It didn't work!!
I stayed about 2 hours checking the code and the wires and I couldn't find thr wrong thing. After relaxing and concentrarting on the circuit I found out that I switched the VCC and TRIG wires cause they are aside. when I put them in the right pins, It works.
That wil help me to write down the code on my final project independtly.
working on the smart home project at saturday