I have a problem with a my code that results in a "Wrong argument type to unary minus" message when I compile.
I am trying to get a stepper motor to turn the other way in this line. I copied the code from an official example that compiled and worked for me without a problem.
The project is experimenting with using potentiometers to change how far a stepper rotates and the speed of rotation.
I am new to coding, I've tried reviewing the help files, google etc. I have changed the variable type form floating to integer, and also tried using a couple of other approaches to get the step value to go negative (I discovered a couple of new errors with that approach)
The code is below. The problem line is at the end:
myStepper.step(-steps);
Thanks for your help,
// the setup routine runs once when you press reset:
// Stepper - Version: Latest
#include <Stepper.h>/* Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board and Arduino UNO. More info: https://www.makerguides.com */
// Modify to control speed and no. revolutions with potentiometers
// Include the Arduino Stepper.h library:
// Define number of steps per rotation:
const int stepsPerRevolution = 2048;
// Wiring:
// Pin 8 to IN1 on the ULN2003 driver
// Pin 9 to IN2 on the ULN2003 driver
// Pin 10 to IN3 on the ULN2003 driver
// Pin 11 to IN4 on the ULN2003 driver
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
int steps();
int RPM();
int reverse(-1);void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}void loop() {
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to RPM (0 - 20 RPM):
float RPM = sensorValue * (20.0 / 1023.0);
// print out the value you read for troubleshooting:
Serial.println(RPM);
Serial.println("RPM");
delay(10);
// Set the speed to equal rpm:
myStepper.setSpeed(RPM);
}{
{
// read the input on analog pin 0:
int sensorValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to steps (0 - 20 RPM):
int steps= sensorValue * (stepsPerRevolution / 1023.0);
// print out the value you read for troubleshooting:
Serial.println(steps);
Serial.println("Steps");
delay(10);
}// Step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(steps);
delay(500);// Step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-steps);
delay(500);
}
}