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.
The problem I am having is that my stepper motor is vibrating and not rotating. What have I done wrong? How can I fix it
I believe the error might be in my moveFowardbackward functions.
My 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;
Serial.begin;
void loop
int soundVal analogReadsoundPin; Read microphone sensor value
Serial.printlnsoundVal;
Check for sound and magnet sensor states
if soundVal && digitalReadmagnetPin LOW Sound detected, and at stopping position
moveForward;
else if soundVal && digitalReadmagnetPin LOW Sound detected, and at initial position
moveBackward;
void moveForward
while digitalReadmagnetPin LOW Keep moving until stopping position is reached
if millis lastStepTime debounceDelay
myStepper.step;
lastStepTime millis;
void moveBackward
while digitalReadmagnetPin LOW 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