I'm working with the Adafruit Motor Shield v2 for a 3D printer project. I got the code up and running and decided to make structs for each motor. When trying this, I got the error that my struct "does not name a type". I'm pretty sure my code is right as I gave experience in C. The code and error messages are below.
Any help would be greatly appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
struct stepper
{
const int id; // motor number
Adafruit_MotorShield shield;
const int totalSteps;
int currStep;
Adafruit_StepperMotor *controller;
};
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 2);
struct stepper *x = NULL;
x->id = 1;
x->shield = AFMS;
x->totalSteps = 250;
x->controller = myMotor1;
struct stepper *y = NULL;
y->id = 2;
x->shield = AFMS;
x->totalSteps = 250;
x->controller = myMotor2;
struct stepper *z = NULL;
z->id = 1;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
x->controller->setSpeed(10);
y->controller->setSpeed(10); // rpm
}
void loop() {
Serial.println("Single coil steps");
x->controller->step(10, FORWARD, SINGLE);
y->controller->step(10, FORWARD, SINGLE);
x->controller->step(10, BACKWARD, SINGLE);
y->controller->step(10, BACKWARD, SINGLE);
oop_stepper:24: error: ‘x’ does not name a type
x->id = 1;
^
oop_stepper:25: error: ‘x’ does not name a type
x->shield = AFMS;
^
oop_stepper:26: error: ‘x’ does not name a type
x->totalSteps = 250;
^
oop_stepper:27: error: ‘x’ does not name a type
x->controller = myMotor1;
^
oop_stepper:29: error: ‘y’ does not name a type
y->id = 2;
^
oop_stepper:30: error: ‘x’ does not name a type
x->shield = AFMS;
^
oop_stepper:31: error: ‘x’ does not name a type
x->totalSteps = 250;
^
oop_stepper:32: error: ‘x’ does not name a type
x->controller = myMotor2;
^
oop_stepper:34: error: ‘z’ does not name a type
z->id = 1;
^
exit status 1
‘x’ does not name a type