Help Debugging My Arduino Code

Hello, I am doing a project for my engineering class.Its uses Arduino. I started a project from this site:


It’s gone well but the code seems to have a lot of bugs in it. Any help would be greatly appreciated.
Here is the code:

#include "Stepper.h"
int sensor = 2; // the pin that the sensor is attached to

int state = LOW; // by default, no motion detected

int val = 0; // variable to store the sensor status (value)

int in1Pin = 12; //the pin that the phase 1 is attached to

int in2Pin = 11; //the pin that the phase 2 is attached to

int in3Pin = 10; //the pin that the phase 3 is attached to

int in4Pin = 9; //the pin that the phase 4 is attached to

int step_num =700;

Stepper motor(64, in1Pin, in2Pin, in3Pin, in4Pin);

void setup() {

pinMode(led, OUTPUT); // initialize LED as an output

pinMode(sensor, INPUT); // initialize sensor as an input

pinMode(in1Pin, OUTPUT); // initialize in1pin as an output

pinMode(in2Pin, OUTPUT); // initialize in2pin as an output

pinMode(in3Pin, OUTPUT); // initialize in3pin as an output

pinMode(in4Pin, OUTPUT); // initialize in4pin as an output

motor.setSpeed(300); //speed of the motor }

void loop(){

val = digitalRead(sensor); // read sensor value

if (val == HIGH) { // check if the sensor is HIGH

digitalWrite(led, HIGH); // turn LED ON

motor.step(step_num); //rotate the motor foreword

delay(1000); // delay 300 milliseconds

motor.step(-step_num); //rotate the motor backward

delay(300);

if (state == LOW)

{ state = HIGH; // update variable state to HIGH

}

}

else {

digitalWrite(led, LOW); // turn LED OFF

motor.step(0);

if (state == HIGH) {

state = LOW; // update variable state to LOW

}

}

}

motor.step(0);

if (state == HIGH) {

state = LOW; // update variable state to LOW

}

}

}

Though I’m sure there are people here that would be able to help, this forum is meant for the Particle platform, not really for Arduino. As such, you might find more assistance on a forum dedicated to Arduino.

Regardless of where you post, dumping a bunch of code with the notion:“this isn’t working, can you fix it?” Isn’t going to get you much assistance.
Be clear in defining what you want to do, what it does, or doesn’t, do, and what you’ve tried already.

Furthermore, whenever possible, please use appropriate code formatting. As is, the code is very hard to read, and since there’s a dedicated code formatting button, it’d be great if you used that.

Also, seeing as it looks like some sort of school work, we generally don’t fix other people’s project, but merely guide them in the correct direction. You’ll find this to be true for most all forums.

3 Likes