This code uses a stepper motor that follows the turns of a potentiometer connected to analog input 0. It defines the stepper motor and sets its speed to 30 RPM. In the loop, it reads the analog sensor, calculates the difference from the previous reading, and uses that value to step the motor forward or backward accordingly. It then updates the previous reading variable.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
47 views
#Include : (For Both Circuits)
This code uses a stepper motor that follows the turns of a potentiometer connected to analog input 0. It defines the stepper motor and sets its speed to 30 RPM. In the loop, it reads the analog sensor, calculates the difference from the previous reading, and uses that value to step the motor forward or backward accordingly. It then updates the previous reading variable.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
Code
(for both circuits)
/* * MotorKnob * * A stepper motor follows the turns of a potentiometer * (or other sensor) on analog input 0. * * http://www.arduino.cc/en/Reference/Stepper * This example code is in the public domain. */
#include <Stepper.h>
// change this to the number of steps on your motor #define STEPS 100
// create an instance of the stepper class, specifying // the number of steps of the motor and the pins it's // attached to Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input int previous = 0;
void setup() { // set the speed of the motor to 30 RPMs stepper.setSpeed(30); }
void loop() { // get the sensor value int val = analogRead(0);
// move a number of steps equal to the change in the // sensor reading stepper.step(val - previous);
// remember the previous value of the sensor previous = val; } [Get Code] Description: if (val-previous ) is positive then motor rotates in forward direction ,if (val-prev) is negative, reverse direction.