Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

  1. include <LiquidCrystal_I2C.h>
  2. include <Wire.h>

/* Program 1 REV B

  Updated 6/19/2017 @ 3:39:AM
  Code concept and project design by David H Haffner Sr.

  Stepper program for the 28YBJ-48 (stepper MTR) and ULN2003 driver
  This particular stepper motor is 5.625 degrees per step
  /64

  Speed is controlled by a delay between each step.
  The longer the delay the slower the rotation.
  That delay value is obtained by reading and analog-to-digital
  cover (A0 in this case/10K trimmer POT) which gives a value from 0 to 1023.
  The value is divided by 4 and add 10 for a delay
  in milliseconds:delay(analogRead(0)/4 +10)

  For faster speeds change 10 to say 2.

  This is calculated between every step to vary speed while stepping.

  I incorporated unsigned long int Val, in order read a little bit more of the AN/Map
  A nice feature of unsigned ints: if a val is unsigned, then val / 4 is optimized by
  the compiler into a bit shift, much more efficient than the actual division you would
  get if val was signed.

  Further incorporated a pin array;//read the pushbutton value into a variable
  int sensorVal[] = { digitalRead [2][3] };//SW1 pin2 & SW2 pin3
  This will illuminate LED's #10(W) and LED #12(bl) to indicate that the switches
  on HIGH. 

  Line 3 on the LCD display has a "current pos:" which indicates shaft position both 
  in forward and reverse.

  The commands below will be compiled into machine code and uploaded
  to the microcontroller.

  This is in the public domain.

  Compiled size 6196 bytes.

  • /

  1. define yellow 7 // M1
  2. define orange 5 // M2
  3. define brown 6 // M3
  4. define blue 4 // M4

  5. define CW 2 //Sw1 in schematic
  6. define CCW 3 //Sw2 in schematic

unsigned long int val = (analogRead('...') / 4 + 10);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

void setup() {

  pinMode(10, OUTPUT);// this is the LED pin10 
  pinMode(12, OUTPUT);// this is for LED pin12 

  digitalWrite(CW, 1); // pull up on
  digitalWrite(CCW, 1); // pull up on

  pinMode(blue, OUTPUT);
  pinMode(brown, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(yellow, OUTPUT);

  // all coils off
  digitalWrite(blue, 0);
  digitalWrite(brown, 0);
  digitalWrite(orange, 0);
  digitalWrite(yellow, 0);

  lcd.begin(20, 4);        // initialize the lcd for 20 chars 4 lines, turn on backlight

  // ------- Quick 3 blinks of backlight  -------------
  for (int i = 0; i < 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.print("AN/MAP:");

  lcd.setCursor(12, 0);

  lcd.print("0-1023");

  lcd.setCursor(0, 1);
  lcd.print("Direction:");//CCW or CC

  lcd.setCursor(0, 3);
  lcd.print("V3.01 Turret Control");//4th line for version display

  lcd.setCursor(0, 2);
  lcd.print("CurrentPos:");

  Serial.begin(115200);

}

void loop() {

  if (!digitalRead(CW))  {
    forward(10);//rpm
    all_coils_off();
  }

  if (!digitalRead(CCW))  {

    reverse(10);//rpm
    all_coils_off();
  }

  //read the pushbutton value into a variable
  int sensorVal_1 = digitalRead(CW);//SW pin
  //print out the value of the pushbutton
  Serial.println(sensorVal_1);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal_1 == HIGH) {
    digitalWrite(10, LOW);//LED pin
  } else {
    digitalWrite(10, HIGH);//LED pin
  }
  //read the pushbutton value into a variable
  int sensorVal_2 = digitalRead(CCW);//SW pin
  //print out the value of the pushbutton
  Serial.println(sensorVal_2);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal_2 == HIGH) {
    digitalWrite(12, LOW);//LED pin
  } else {
    digitalWrite(12, HIGH);//LED pin
  }

} // end loop

void all_coils_off(void) {

  digitalWrite(blue, 0);
  digitalWrite(brown, 0);
  digitalWrite(orange, 0);
  digitalWrite(yellow, 0);

}

void reverse(int i) {

  {
    lcd.setCursor(10, 1);
    lcd.print("<<CCW");
  }
  while (1)   {

    digitalWrite(blue, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
    digitalWrite(blue, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(7, 0);
      lcd.print(analogRead('...') / 4 + 10);
    }
    digitalWrite(blue, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
    digitalWrite(blue, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
  }

}

void forward(int i) {

  {
    lcd.setCursor(10, 1);
    lcd.print("CW>>>");
  }
  while (1)   {

    digitalWrite(blue, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
    digitalWrite(blue, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
    digitalWrite(blue, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
    digitalWrite(blue, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(analogRead('...') / 4 + 10);
    i--;
    if (i < 1) break;
    {
      lcd.setCursor(12, 2);//print out the value of the pushbutton
      lcd.print(i--);
    }
  }

}