Answered step by step
Verified Expert Solution
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 backeards to the magnet sensor that was at its original position. What am I missing? What should I improve?
Code I have to far:
#include
const int soundPin ; Analog pin for microphone sensor
const int magnetPin; Digital pin for initial position magnet
const int magnetPin; Digital pin for stopping position magnet
const int stepsPerRevolution ; Steps per revolution for BYJ stepper motor
Stepper myStepperstepsPerRevolution; Define stepper motor pins
int speed ; Stepper motor speed
void setup
pinModesoundPin INPUT;
pinModemagnetPin INPUT;
pinModemagnetPin INPUT;
myStepper.setSpeedspeed;
void loop
int soundVal analogReadsoundPin; Read microphone sensor value
Check for sound and magnet sensor states
if soundVal && digitalReadmagnetPin Sound detected and not at stopping position
moveForward;
else if soundVal && digitalReadmagnetPin Sound detected and at stopping position
moveBackward;
else
myStepper.stop; Stop motor if no sound or at initial position
void moveForward
myStepper.stepstepsPerRevolution; Move forward one revolution
delay; Delay for seconds
void moveBackward
myStepper.stepstepsPerRevolution; Move backward one revolution
delay; Delay for seconds
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started