arduino easyriver code problel


will build the telescope, and I need the code but I do not really understand how.
maybe someone here can help me.
I have the code but it does not work. something wrong :))

#define DISTANCE 3200

int StepCounter = 0;

int Stepping = false;

void setup() {

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

pinMode(2, INPUT);

pinMode(3, INPUT);

}

void loop() {

if (digitalRead(3) == LOW && Stepping == false)

{

digitalWrite(8, LOW);

Stepping = true;

}

if (digitalRead(2) == LOW && Stepping == false)

{

digitalWrite(8, HIGH);

Stepping = true;

}

if (Stepping == true)

{

digitalWrite(9, HIGH);

delay(1);

digitalWrite(9, LOW);

delay(1);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)

{

StepCounter = 0;

Stepping = false;

}

}

}

It is poor code but "I have the code but it does not work. something wrong" is insufficient.

What does the program do? What did you expect it to do? How do you know that you do not have a hardware problem? What have you tried?

You may find these links useful
Stepper Motor Basics
Simple Stepper Code

Post a link to the datasheet for your stepper motor.

What power supply are you using for the motor (volts and amps)?

...R

forgive me I put the wrong picture and the code in a hurry
this problem and the code image

// Example5 code for Brian Schmalz's Easy Driver Example page
// http://www.schmalzhaus.com/EasyDriver/EasyDriverExamples.html

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define LEFT_PIN 4
#define STOP_PIN 3
#define RIGHT_PIN 2

// Define our analog pot input pin
#define SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 500
#define MIN_SPEED 0.1

void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);

// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.

// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}

// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}

// This will run the stepper at a constant speed
stepper1.runSpeed();
}

the problem is that the program does not understand the code"error translate?"
BYJ48 Stepper Motor 28ybt-48 5vdc

I want s1, s2, s3 to turn the button around the engine, right, left and stop
potentiometer adjust the speed..
forgive my English but hopefully you'll understand:)

tm1979:
forgive me I put the wrong picture and the code in a hurry
this problem and the code image

Did you read Replies #1 and #2 before you posted Reply #3?

And please modify your posts and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

thanks for the advice .. maybe I'm a bit wiser again :slight_smile:
I made a new code but not working either.
hopefully look a bit better :slight_smile:

#define DISTANCE 3200

int Distance = 0;  // Record the number of steps we've taken
int StepCounter = 0;

int Stepping = false;

#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  SPEED_PIN 0
#define  MAX_SPEED 500
#define  MIN_SPEED 0.1



int pos1 = 3600;

int pos2 = 5678;

void setup() {

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);
  



  pinMode(2, INPUT);

  pinMode(3, INPUT);
  
  pinMode(4, INPUT);
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);

}


void loop() 

{

  if (digitalRead(3) == LOW && Stepping == false)

  {

    digitalWrite(9, HIGH);

    Stepping = true;

  }

  if (digitalRead(2) == LOW && Stepping == false)

  {

    digitalWrite(9, LOW);

    Stepping = true;

  }


  if (Stepping == true)

  {

    digitalWrite(9, HIGH);

    delay(1);

    digitalWrite(9, LOW);

    delay(1);


    StepCounter = StepCounter + 1;


    if (StepCounter == DISTANCE)

    {
     {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) }
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) }    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) }
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    Stepper.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed
    Stepper.setSpeed(current_speed);
  }


      StepCounter = 0;

      Stepping = false;

    }

  }

}

error:expecte declaration before "}" token

tm1979:
I made a new code but not working either.

error:expecte declaration before "}" token

Do you mean it won't compile or it won't work? The solutions are very different.

My guess is that you have a compile error.

You need to post the full error message with the line number.

Have you carefully read the lines of code immediately before the error to check for a typo?

If you can compile and upload the program then you need to tell us what it actually does and what you want it to do that is different. "Not working" is no help for diagnosing a problem.

...R
PS. You have not posted a link to the datasheet for your motor.

This

    if (StepCounter == DISTANCE)

    {
     {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) }
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) }    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) }
    sign = 0;
  }

makes it look like curly brackets are being thrown in at random. Curly brackets have an important purpose in a C/C++ program and are not at all random.

I also notice that sometimes there is a comparison with LOW and sometimes there is a comparison with 0. Who wrote this code? How many programs have been thrown together? My questions have still not been answered, so I quit.