Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following arduino code to program a car to move when it gets signalled by a sound sensor and stops when it reaches

I have the following arduino code to program a car to move when it gets signalled by a sound sensor and stops when it reaches a magnet sensor. The car should move foward when signalled by sound sensor then stop when it reaches magnet sensor in front. Then when signalled again by the sound sensor it should move backwards to the magnet sensor that was at its original position. Furthermore, the care is moving foward to one magent sensor then backwards to another . Will the code I have achieve what I need to?What am I missing? What should I improve?
Code I have to far:
#include
const int soundPin =2; // Analog pin for microphone sensor
const int magnetPin1=3; // Digital pin for initial position magnet
const int magnetPin2=4; // Digital pin for stopping position magnet
const int stepsPerRevolution =2048; // Steps per revolution for 28BYJ-48 stepper motor
Stepper myStepper(stepsPerRevolution,8,9,10,11); // Define stepper motor pins
int speed =100; // Stepper motor speed
void setup(){
pinMode(soundPin, INPUT);
pinMode(magnetPin1, INPUT);
pinMode(magnetPin2, INPUT);
myStepper.setSpeed(speed);
}
void loop(){
int soundVal = analogRead(soundPin); // Read microphone sensor value
// Check for sound and magnet sensor states
if (soundVal >500 && !digitalRead(magnetPin2)){// Sound detected and not at stopping position
moveForward();
} else if (soundVal >500 && digitalRead(magnetPin2)){// Sound detected and at stopping position
moveBackward();
} else {
myStepper.stop(); // Stop motor if no sound or at initial position
}
}
void moveForward(){
myStepper.step(stepsPerRevolution); // Move forward one revolution
delay(2000); // Delay for 2 seconds
}
void moveBackward(){
myStepper.step(-stepsPerRevolution); // Move backward one revolution
delay(2000); // Delay for 2 seconds
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Do you avoid accusing others when something goes wrong?

Answered: 1 week ago

Question

1. Give extra points for correct and creative answers.

Answered: 1 week ago