Please could you write a programming code for ARDUINO Nano - for a smart walking stick. Using these parts : An Arduino nano An Ultrasonic sensor(HCSR04) A 9-volt Battery A9-volt Battery connector A Buzzer Some jumper wires A vibration motor A switch The programming code should be similar to the one below, but please change it up and write comments throughout // defines pins numbers const int trigPin = 9; const int echoPin = 10; const int buzzer =11; const int motor = 13; // defines variables long duration; int distance; int safetyDistance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(buzzer, OUTPUT); pinMode(motor, OUTPUT); Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; safetyDistance = distance; if (safetyDistance <= 18) { digitalWrite(buzzer, HIGH); digitalWrite(motor, HIGH); } else { digitalWrite(buzzer, LOW); digitalWrite(motor, LOW); } // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance);
Please could you write a
Using these parts :
- An Arduino nano
- An Ultrasonic sensor(HCSR04)
- A 9-volt Battery
- A9-volt Battery connector
- A Buzzer
- Some jumper wires
- A vibration motor
- A switch
The programming code should be similar to the one below, but please change it up and write comments throughout
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer =11;
const int motor = 13;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup()
{
pinMode(trigPin, OUTPUT);
// Sets the trigPin as an Output
pinMode(echoPin, INPUT);
// Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(motor, OUTPUT);
Serial.begin(9600);
// Starts the serial communication
}
void loop()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
safetyDistance = distance;
if (safetyDistance <= 18)
{
digitalWrite(buzzer, HIGH);
digitalWrite(motor, HIGH);
}
else
{
digitalWrite(buzzer, LOW);
digitalWrite(motor, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Step by step
Solved in 3 steps









