Please help newbie with first small sketch.

Trying to write first sketch. I seem to missing something. Is my syntax wrong?

void setup()
{
int FREE pinMode(7, OUTPUT); //  FREE = high.  This is the enable pin on my servo driver IC - free means I can move it by hand
int HOME pinMode(2,INPUT_PULLUP); // HOME SENSOR - active LOW
int END pinMode(3,INPUT_PULLUP); //  END SENSOR - active LOW
int DIR pinMode(12, OUTPUT); // DIRECTION RIGHT = 1 or LEFT =0
int STEP = pinMode(13, OUTPUT);  // set digital pin 13 as STEP PIN
int SPEED = 250;  //  sets 250ms delay
  }

void loop()

  digitalWrite(FREE, LOW);  //Enables stepper driver
  if (HOME == LOW) {digitalWrite(DIR, HIGH)}; if (END == LOW) {digital Write (DIR, LOW)}
  {
  digitalWrite(STEP, HIGH); // sets the STEP pin on servo driver HIGH
  delay(SPEED);             // waits for 250 milliseconds
  digitalWrite(ledPin, LOW);// sets the STEP pin off
  delay(SPEED);             // waits for 250 milliseconds
  if (HOME == LOW) digitalWrite(FREE, HIGH); if (END == LOW) digital Write(FREE, HIGH)
}

Is my syntax wrong?

Yes, very.

It's hard to know where to begin. Which of the examples have you worked through?

I have been looking at a bunch of examples. I'm still not sure what needs to be above setup and what needs to be in the setup.

Stuff outside setup and loop is stuff that needs to be persistent and/or known and visible throughout the life of the sketch. I don't understand what you have done inside setup - it doesn't like any of the examples you have been successful with.

This is so wrong it is hard to know where to start. Most of the main loop is just nonsense but this compiles. Don't let that fool you it will still do nothing.

int FREE = 7;
int HOME = 2;
int END =3;
int DIR = 12;
int STEP = 13;
int SPEED;

void setup()
{
 pinMode(FREE, OUTPUT); //  pin 7 set to output
 pinMode(HOME,INPUT_PULLUP);
 pinMode(END,INPUT_PULLUP); 
 pinMode(DIR, OUTPUT); 
 pinMode(STEP, OUTPUT);  // set digital pin 13 as STEP PIN
 SPEED = 250;  //  sets 250ms delay
  }

void loop()
{ // ,missed this one
  digitalWrite(FREE, LOW);  //Enables stepper driver
  // this next line is just rubbish
  // if (HOME == LOW) {digitalWrite(DIR, HIGH)}; if (END == LOW) {digitalWrite(DIR, LOW)}
 // { // what is this doing ?
  digitalWrite(STEP, HIGH); // sets the STEP pin on servo driver HIGH
  delay(SPEED);             // waits for 250 milliseconds
  // digitalWrite(ledPin, LOW);// sets the STEP pin off - ledPin never defined so it will fail here
  delay(SPEED);             // waits for 250 milliseconds
  if (HOME == LOW) digitalWrite(FREE, HIGH); if (END == LOW) digitalWrite(FREE, HIGH);
}

Grumpy_Mike: This is so wrong it is hard to know where to start. Most of the main loop is just nonsense but this compiles. Don't let that fool you it will still do nothing.

int FREE = 7;
int HOME = 2;
int END =3;
int DIR = 12;
int STEP = 13;
int SPEED;

void setup() { pinMode(FREE, OUTPUT); //  pin 7 set to output pinMode(HOME,INPUT_PULLUP); pinMode(END,INPUT_PULLUP); pinMode(DIR, OUTPUT); pinMode(STEP, OUTPUT);  // set digital pin 13 as STEP PIN SPEED = 250;  //  sets 250ms delay   }

void loop() { // ,missed this one   digitalWrite(FREE, LOW);  //Enables stepper driver   // this next line is just rubbish   // if (HOME == LOW) {digitalWrite(DIR, HIGH)}; if (END == LOW) {digitalWrite(DIR, LOW)} // { // what is this doing ?   digitalWrite(STEP, HIGH); // sets the STEP pin on servo driver HIGH   delay(SPEED);            // waits for 250 milliseconds   // digitalWrite(ledPin, LOW);// sets the STEP pin off - ledPin never defined so it will fail here   delay(SPEED);            // waits for 250 milliseconds   if (HOME == LOW) digitalWrite(FREE, HIGH); if (END == LOW) digitalWrite(FREE, HIGH); }

Thank you guys for helping me! I am new to this and really value your help!

I want to control a stepper motor with a DRV8825 stepper motor driver. The enable pin, active low connected to pin 7 when HIGH makes motor in standby(FREE).

I want to change DIRECTION when HOME or END limit switch active low.

STEP pin pulses.

At end I want it to go back to standby(FREE)

the following line was to set DIRECTION based on HOME or END position limit switches

if (HOME == LOW) {digitalWrite(DIR, HIGH)}; if (END == LOW) {digitalWrite(DIR, LOW)

That last line should be

 if (HOME == LOW) digitalWrite(DIR, HIGH); 
if (END == LOW) digitalWrite(DIR, LOW);

But you are mixing up the Pin number which is what HOME and END are with the value found on that pin. So you need

That last line should be

 if (digitalRead(HOME) == LOW) digitalWrite(DIR, HIGH); 
if (digitalRead(END) == LOW) digitalWrite(DIR, LOW);

Simallarly to pulse the STEP pin write HIGH to it and then write LOW to it.

Thanks a bunch for your help! I still have a few questions. I'll try to make them good.

Grumpy_Mike: That last line should be

 if (HOME == LOW) digitalWrite(DIR, HIGH); 
if (END == LOW) digitalWrite(DIR, LOW);

But you are mixing up the Pin number which is what HOME and END are with the value found on that pin. So you need

That last line should be

 if (digitalRead(HOME) == LOW) digitalWrite(DIR, HIGH); 
if (digitalRead(END) == LOW) digitalWrite(DIR, LOW);

Simallarly to pulse the STEP pin write HIGH to it and then write LOW to it.

Hi guys,

Am I anywhere close? I just want to move stepper motor between two limit switches at adjustable speed. Also want to be able to manually move stepper motor at beginning before automatic movement and after hitting limit switch at end of travel.
I have an UNO and an Adafruit 1602 LCD with buttons on order. I hope to try a run of this Saturday.

int FREE = 7;
int HOME = 2;
int END =3;
int DIR = 12;
int STEP = 13;
int SPEED;

void setup()
{
 pinMode(FREE, OUTPUT); // set digital pin 7 as output - LOW = Enable / HIGH=Disable stepper motor (FREE)
 pinMode(HOME,INPUT_PULLUP); //HOME limit switch
 pinMode(END,INPUT_PULLUP); // END limit Switch
 pinMode(DIR, OUTPUT); // set digital pin 12 as DIRECTION
 pinMode(STEP, OUTPUT);  // set digital pin 13 as STEP PIN
 SPEED = 250;  //  sets 250ms delay
  }

void loop()
{ 
  uint8_t buttons = lcd.readButtons();
     
    if (buttons) {
    if (buttons & BUTTON_UP) {
    SPEED==SPEED +10; lcd.print(SPEED);
    }
    if (buttons & BUTTON_DOWN) {
    SPEED==SPEED -10; lcd.print(SPEED);
    }
    if (buttons & BUTTON_SELECT) {
    driveMotor();  // calls driveMotor function
    }
  
  
  void driveMotor()
  { // enter driveMotor function
  digitalWrite(FREE, LOW);  //Enables stepper driver
  if (digitalRead(HOME) == LOW) digitalWrite(DIR, HIGH); // read limit switches to determin DIRection 
  if (digitalRead(END) == LOW) digitalWrite(DIR, LOW); // read limit switches to determin DIRection
  digitalWrite(STEP, HIGH); // sets the STEP pin on servo driver HIGH
  delay(SPEED);             // waits for 250 milliseconds
  digitalWrite(STEP, LOW);// sets the STEP pin on the servo driver LOW
  delay(SPEED);             // waits for 250 milliseconds
  if (digitalRead(HOME) == LOW) digitalWrite(FREE, HIGH);// IF HOME limit switch activated, set motor off
  if (digitalRead(END) == LOW) digitalWrite(FREE, HIGH); // IF END limit switch activated, set motor off
   
} //return to where driveMotor function was called

No that will not compile why have you not tried to compile it? The line  uint8_t buttons = lcd.readButtons(); What's that all about then? It is calling up some libary that you have not declaired or initialised. And wht the F### do you need a buttons libary anyway or an lcd libary at that. Just read the digital lines your push buttons are connected to.

Grumpy_Mike: No that will not compile why have you not tried to compile it? The line  uint8_t buttons = lcd.readButtons(); What's that all about then? It is calling up some libary that you have not declaired or initialised. And wht the F### do you need a buttons libary anyway or an lcd libary at that. Just read the digital lines your push buttons are connected to.

Yes I will have to add a library. I'm sorry, I should have done better. I'm trying to learn. I don't even have my UNO yet. I will have it and trying things out Saturday. I want this to be programmable in the field. So I need a display and buttons. Other than the buttons am I anywhere near working code? Is my "flow" OK? Is anything OK?

Is anything OK?

Not much.

You do not need an arduino to compile code, just hit the verify button on the IDE window.

Yea! This compiles! Don’t know if it will work right but it does compile. I still have MUCH to learn!!!

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

int FREE = 7;
int HOME = 2;
int END =3;
int DIR = 12;
int STEP = 13;
int SPEED;

void setup()
{
 pinMode(FREE, OUTPUT); // set digital pin 7 as output - LOW = Enable / HIGH=Disable stepper motor (FREE)
 pinMode(HOME,INPUT_PULLUP); //HOME limit switch
 pinMode(END,INPUT_PULLUP); // END limit Switch
 pinMode(DIR, OUTPUT); // set digital pin 12 as DIRECTION
 pinMode(STEP, OUTPUT);  // set digital pin 13 as STEP PIN
 SPEED = 250;  //  sets 250ms delay
  }

void loop()
{ 
  lcd.begin(16, 2);
  lcd.readButtons();
     
    {
    if (BUTTON_UP) {
    SPEED==SPEED +10; lcd.print(SPEED);
    }
    if (BUTTON_DOWN) {
    SPEED==SPEED -10; lcd.print(SPEED);
    }
    if (BUTTON_SELECT) {
    driveMotor(); } // calls driveMotor function
    }
	}
  
  
  void driveMotor() //driveMotor function
  { 
  digitalWrite(FREE, LOW);  //Enables stepper driver
  if (digitalRead(HOME) == LOW) digitalWrite(DIR, HIGH); // read limit switches to determin DIRection 
  if (digitalRead(END) == LOW) digitalWrite(DIR, LOW); // read limit switches to determin DIRection
  digitalWrite(STEP, HIGH); // sets the STEP pin on servo driver HIGH
  delay(SPEED);             // waits for 250 milliseconds
  digitalWrite(STEP, LOW);// sets the STEP pin on the servo driver LOW
  delay(SPEED);             // waits for 250 milliseconds
  if (digitalRead(HOME) == LOW) digitalWrite(FREE, HIGH);// IF HOME limit switch activated, set motor off
  if (digitalRead(END) == LOW) digitalWrite(FREE, HIGH); // IF END limit switch activated, set motor off
 }//return to where driveMotor function was called

Please use Tools + Auto Format before posting code.

There are still a lot of problems with that code.

int FREE = 7;
int HOME = 2;
int END =3;
int DIR = 12;
int STEP = 13;
int SPEED;

By convention, all capital letter names are reserved for constants. If these ARE constants, the const keyword should be used. SPEED is clearly NOT constant.

    {

Useless open brace.

    SPEED==SPEED +10; lcd.print(SPEED);

One line; many problems. First, it's ONE statement per line. Second, comparing the value of SPEED to SPEED+10 is ALWAYS going to return false. Third, you have to wonder if that's really what that statement is doing. Is it comparing SPEED to SPEED + 10? Or, is it comparing SPEED to SPEED, and adding 10 to the result? Use parentheses to make things clear. And, then see problem #2.

There is still a lot wrong. This is how the loop function might look:-

void loop()
{ 
  lcd.readButtons();
     lcd.begin(16, 2); // ony needs doing once so move it to the setup fnction
    {
    if (BUTTON_UP) {
     SPEED=SPEED +10;  // only one = sign
      lcd.print(SPEED);
    }
    if (BUTTON_DOWN) {
      SPEED==SPEED -10; // only one = sign
      lcd.print(SPEED);
    }
    if (BUTTON_SELECT) {
       driveMotor(); } // calls driveMotor function
    }
    }

Look at the comments and move that first line.

However will it work? Well no not like you want.

First of all you are changing the speed value all the time the button is held down, you only want to change it when the button makes a transition from released to pressed.

Second, you are going to take half a second per step on the motor, that is going to move it about 3.6 degree per second so it will be very slow.

Third when you do hit the end stop, yes you will disable the motor but then when the driveMotor function is called again you will enable and step the motor just like it had not triggered the end stop, thus driving it past the end stop.

Fourth it will only step while you hold down the select button, release the button and it will stop.