Trying to implement an idea similar to a graduation project.
TinkerCad
Experiment with the shape of the connections
Software
Arduino Uno
To write the code taken from TinkerCAD and transfer it to be implemented using Arduino board
Electrical tools:
Jumper Wires
Experiment with connecting the electrical circuit on Tinkercad
1-Make a mind map .
3-Writing code on Arduino
4-I started trying each part of the components alone first.. does it work or not?
5-All the individual codes of the components are combined with ... under the if condition of using the switch.
What does the code actually do?
Case 1: Manual control
If you press the Control button, you can manually control the servo by rotating the potentiometer.
Case 2: Sensor control
If you do not press the button, the sensor measures the distance:
If the distance is less than 20 cm, the servo is moved to 180° and the LED lights up.
If the distance is greater than 20 cm, the servo returns to 0° and the distance is displayed on the Serial Monitor.
#include <Servo.h>
Servo servo;
int potpin = A1;
const int SERVO_PIN = 3;
const int trig_pin = 9;
const int echo_pin = 10;
int led = 7;
int Control = 8;
const int DISTANCE_THRESHOLD = 20;
float duration_us, distance_cm;
void setup() {
servo.attach(SERVO_PIN);
servo.write(0);
pinMode(led, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(trig_pin, OUTPUT);
pinMode(Control, INPUT_PULLUP);
digitalWrite(trig_pin, LOW);
Serial.begin(9600); //
Serial.println("System Started...");
}
void loop() {
int controlState = digitalRead(Control);
Serial.print("Control Button State: ");
Serial.println(controlState);
if (controlState == LOW) {
Serial.println("Manual Mode (Potentiometer Control)");
int val = analogRead(potpin);
Serial.print("Potentiometer Value: ");
Serial.println(val);
val = map(val, 0, 1023, 0, 180);
Serial.print("Mapped Servo Angle: ");
Serial.println(val);
servo.write(val);
delay(15);
} else {
Serial.println("Auto Mode (Ultrasonic Sensor)");
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
duration_us = pulseIn(echo_pin, HIGH);
distance_cm = 0.017 * duration_us;
Serial.print("Measured Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
if (distance_cm > 0 && distance_cm < DISTANCE_THRESHOLD) {
Serial.println("Object Detected - Activating Servo & LED");
servo.write(180);
digitalWrite(led, HIGH);
delay(3000);
} else {
Serial.println("No Object Detected - Resetting Servo & LED");
servo.write(25);
digitalWrite(led, LOW);
}
}
delay(200);
}
6-Finally, there were some grammatical errors. I had ChatGPT review them and send the code without errors.
First:
Definition of pins per component.
servo : pin 3, 9,10 .... it will work by potentiometer (pin A1)
on/ off switch on pin 8
void setup:
Definition of servo:
initial angel for the servo (0) ....
Definition of servo inputs
Definition of LED: It is an output.
Followed on serial
void loop:
if (1) main
If the switch is closed (low) , the manual mode will be activated to control via the potentiometer.
Use the map to convert the equivalent of the motion of thepotentiometer with the servo angles.
else:
if (2) ( the switch was high ):
Approaching the sensor 20cm , the server opens 180... and the LED lights up.
Preparing the tools
baby steps
Extending the wire and connecting the electrical circuit and the card board model
Finally😮💨🤎
Manual mode
Automatic mode
Youssef also helped me adjust the start and direction of the manual servo.
Problem:
When running the code .. servo motor does not work.😑
what i do ? : Chat GPT was asked about how to properly connect a motor and how to operate it using the code...
I followed the steps and it was operated🥳
The Reason:
The red wire is not connected to 5 volts.
Technical Solution :
I connected the red wire to 5 volt
The idea of using more than input and output... simulates the graduation project.
The special session task that helped me in this assignment🥰