Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Goal is to create an arduino program that turns on a stepper motor with a sound sensor and it rotate continuously until a magnet sensor
Goal is to create an arduino program that turns on a stepper motor with a sound sensor and it rotate continuously until a magnet sensor is activated and it shuts off. Then once the sound sensor is activated again it goes into the reverse direction until it approaches the other magnet.
Why isn't my code working?
Code:
#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
unsigned long lastStepTime ; Time of last step
unsigned long debounceDelay ; Debounce delay in milliseconds
void setup
pinModesoundPin INPUT;
pinModemagnetPin INPUTPULLUP;
pinModemagnetPin INPUTPULLUP;
myStepper.setSpeedspeed;
void loop
int soundVal analogReadsoundPin; Read microphone sensor value
Check for sound and magnet sensor states
if soundVal && digitalReadmagnetPin && digitalReadmagnetPin
Sound detected, not at stopping position, and not at initial position
moveForward;
else if soundVal && digitalReadmagnetPin && digitalReadmagnetPin
Sound detected, not at initial position, and at stopping position
moveBackward;
void moveForward
while digitalReadmagnetPin Keep moving until stopping position is reached
if millis lastStepTime debounceDelay
myStepper.step;
lastStepTime millis;
void moveBackward
while digitalReadmagnetPin Keep moving until initial position is reached
if millis lastStepTime debounceDelay
myStepper.step;
lastStepTime millis;
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