Hello Everyone
I have built a rotary car dumper for my HO scale railroad layout.
I have the sketch below operating almost the way I want it to but.
1- I would like for the red led to come on solid after the first button push.
2- Once the rotary makes it to the dumped position the red led goes to flashing, which is functioning correctly.
3- Then when the second button push is made, I would like for the red led go to solid again while the rotary is moving.
4- Then when the rotary is back to the start positon the red led goes off.
5- Then the green light comes on to signal car is empty and ready for a new one to be pushed in, this part is functioning correctly.
As the code is written now with the first button push the green led goes off, which is correct.
when the rotary makes it to the dumping point the red led starts flashing. which is correct
when the rotary makes it back to the start point the green light back comes on. which is correct.
I have inserted digitalWrites(ledPin1, High); at various points in the sketch but have had no success making it function like I want
it to.
any help would be appreciated Thanks James
// constants won't change. Used here to set a pin number:
const int ledPin1 = 3;// the number of LED1 which is thye red led
const int ledPin2 = 4;// the number of LED2 which is the green led
// Variables will change:
int led1State = LOW; // ledState used to set LED1
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 500; // interval at which to blink (milliseconds)
/*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino with the Accelstepper library the original sketch did not use a library. More info: https://www.makerguides.com */
/* I modified the sketch from makerguides to run a nema 17 steppermotor with an A4988 driver and a 28BYJ-48 steppermotor with a ULN2003a driver board to run in a sequence with a button push then return to its start position with the second button push*/
/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <AccelStepper.h>
// Define stepper motor connections :
#define dirPin 5
#define stepPin 6
// Define step constant use a value of 4 for full step use a value of 8 for halfsteps for the unl2003a driver board
//#define FULLSTEP 4
#define HALFSTEP 8
// Creates an instance for stepper 1
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper1(HALFSTEP, 8, 10, 9, 11);
// creates an instance for stepper 0
AccelStepper stepper0(1, stepPin, dirPin);
#define buttonPin 7 // defines the push button pin;
//Define variables
//variable that stores the state of the button:
int buttonReading;
//State of the blinds, low is closed, high is open:
int state = LOW;
int previous = LOW;
/*-----( Declare Constants )-----*/
// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
/*-----( Declare objects )-----*/
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
/*-----( Declare Variables )-----*/
int status;
void setup() {
Serial .begin(9600);
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, HIGH);
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
//-------- Write characters on the display ------------------
// NOTE: Cursor Position: Lines and Characters start at 0
lcd.setCursor(0, 0); //Start at character 4 on line 0
lcd.print("Hi Model RailRoaders");
delay(500);
lcd.setCursor(0, 1);
lcd.print("From The I&J RR");
delay(500);
lcd.setCursor(0, 2);
lcd.print("Rotary Dump Ready");
lcd.setCursor(0, 3);
delay(500);
lcd.print("Push Button to Dump");
pinMode (buttonPin, INPUT_PULLUP);
// set the maximum speed, acceleration factor, initial speed and the start position for stepper 1
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setCurrentPosition(0);
// set the maximum speed, acceleration factor, initial speed and the start position for stepper 0
stepper0.setMaxSpeed(600);
stepper0.setAcceleration(50.0);
stepper0.setCurrentPosition(0);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (led1State == LOW) {
led1State = HIGH;
} else {
led1State = LOW;
}
}
buttonReading = digitalRead(buttonPin);
if (buttonReading == LOW && previous == HIGH) {
if (state == LOW) {
state = HIGH;
}
else {
state = LOW;
}
}
previous = buttonReading;
if (state == HIGH) {
// set LED1 with the ledState of the variable:
digitalWrite(ledPin1 , led1State);
digitalWrite(ledPin2 , LOW);
while (stepper1.currentPosition() != 2000) // adjust this number for the go to position;
{
stepper1.setSpeed(600);
stepper1.runSpeed();
}
// Serial.println("clamping car");
lcd.setCursor(0, 0); //Start at character 4 on line 0
lcd.print("Hi Model RailRoaders");
lcd.setCursor(0, 1);
lcd.print("From The I&J RR");
lcd.setCursor(0, 2);
lcd.print("Rotary Dump Running");
lcd.setCursor(0, 3); //Start at character 4 on line 0
lcd.print("Dumping Car ");
// delay(2000);
}
if (state == HIGH) {
while (stepper0.currentPosition() != 6000) // adjust this number for the go to position;
{
stepper0.setSpeed(600);
stepper0.runSpeed();
}
//Serial.println("rotating car");
}
if (state == LOW) {
while (stepper0.currentPosition() != 0)
{
stepper0.setSpeed(-600);
stepper0.runSpeed();
}
//Serial.println("rotating car back");
}
if (state == LOW) {
while (stepper1.currentPosition() != 0)
{
stepper1.setSpeed(-600);
stepper1.runSpeed();
}
//Serial.println("Ready for loaded car");
lcd.setCursor(0, 0); //Start at character 4 on line 0
lcd.print("Hi Model Railroaders");
lcd.setCursor(0, 1);
lcd.print("From The I&J RR");
lcd.setCursor(0, 2);
lcd.print("Rotary Dump Ready ");
lcd.setCursor(0, 3); //Start at character 4 on line 0
lcd.print("Push Button To Dump");
// delay(2000);
digitalWrite(ledPin1 , LOW);
digitalWrite(ledPin2 , HIGH);
}
}