ArduinoA
Arduino2mo ago
2 replies
Spaceman

Coding weirdness

Hello i am jsut learning to code arduino i have gotten a stepper to move and to change directioons at limits. Now what i am trying to do is that the position of the stepper matches the degree of the pot. i dont know where its going wrong because logically in my head it makes sense read the comments for details. thankyou for the help.
#include <Stepper.h> #include <EEPROM.h> int stepsPerRevolution = 2048; int rpm = 10; int orig; Stepper myStepper (stepsPerRevolution, 8, 10, 9, 11); void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); myStepper.setSpeed(rpm); } void loop() { // reading pot value int sensorValue = analogRead(A0); Serial.print('\n'); Serial.print("pot"); Serial.println(sensorValue); //multiplyin ghte pot value to the stepps of the motor witch is 2048 int deg = sensorValue*2.66; delay(1000); //getting the motor position orig = EEPROM.read(0) + EEPROM.read(1); // write out values Serial.print("deg"); Serial.println(deg); Serial.print("orig"); Serial.println(orig); Serial.print('\n'); //1024 or less then minusing the value of the motor position then save if (deg <= 1024) { int right = deg - orig; myStepper.step(-right); EEPROM.write(1,right); } //1023 or more subtracting 1024 because its higher than 1024 to get a pure step movement number then minusing the value of the motor position then save if (deg >= 1023) { int degup = deg - 1024; int left = degup - orig; myStepper.step(left); EEPROM.write(0,left); }
Was this page helpful?