I incorporated a couple of code changes to the stepper MTR program, one is, now when a button is pressed U can see the voltage displayed on Ln 1 of the LCD in real time, reading is taken from A0 via the ADC/mapping of the directional coils controlling direction.
2nd change is, a 50K trimmer POT, yes I experimented enough and I am convinced that this is the right value and setting for the functions that this stepper MTR program needs to perform.
Here is the code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
/* Program 1 REV B
Updated 6/30/2017 @ 5:30:PM
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/50K 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 illuminat LED's #10(W) and LED #12(bl) to indicate that the switches
on HIGH. These values are then displayed on the LCD menu on line 3 as a monitor
of the switches values.
// set the LCD address to 0x27 or 0x3F for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
The commands below will be compiled into machine code and uploaded
to the microcontroller.
This is in the public domain.
Compiled size 6646 bytes.
*/
const int yellow = 7; // M1
const int orange = 5; // M2
const int brown = 6; // M3
const int blue = 4; // M4
const int CW = 2; //Sw1 in schematic
const int CCW = 3; //Sw2 in schematic
unsigned long int val = (analogRead(A0) / 4 + 10);
unsigned long int val1 = (analogRead('...') * 4 + 2);// scale it to use it with the MTR (value between 0 and 175)
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
// initialize digital pin LED_BUILTIN (13) as an output.
pinMode(LED_BUILTIN, OUTPUT);//Modified "blink" sequence (2)millisec in-step/W/MTR sequence
pinMode(10, OUTPUT);// this is the LED pin for sensor val prgm
pinMode(12, OUTPUT);// this is for LED pin 12 sensor Val prgm
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(16, 0);
lcd.print("0-5V");
lcd.setCursor(0, 1);
lcd.print("Direction:");//CCW or CC
lcd.setCursor(0, 3);
lcd.print("SwitchState:");//4th line for version display
lcd.setCursor(0, 2);
lcd.print("DIV:");
Serial.begin(115200);
}
void loop() {
// read the input on :
for (int i = 0; i < 6; i++) {
val1 = analogRead(i);
delay(10);
// Convert the analog reading (which goes from 0 - 1023) to voltage range (0 - 5V);
float voltage0 = val1 * (5.0 / 1023.0);
// print out the value you read:
Serial.print(voltage0); Serial.print("i =");
Serial.print(i); Serial.print(";");
if (i == 5) Serial.println(" ");
lcd.setCursor(11, 0);
lcd.print(voltage0);//reads the current Voltage from A0
}
if (!digitalRead(CW)) {
forward(10);
all_coils_off();
lcd.setCursor(13, 3);// set Cursor at place 12, 3
lcd.print(CW);
}
if (!digitalRead(CCW)) {
reverse(10);
all_coils_off();
lcd.setCursor(13, 3);// set Cursor at place 12, 3
lcd.print(CCW);
}
{
digitalWrite(LED_BUILTIN, HIGH); // When button is pressed, moves MTR same# of steps as LED timing sequence
delay(1); // wait for a second
digitalWrite(LED_BUILTIN, LOW); //
delay(1); // wait for a second
}
//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 LED pin 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 LED pin 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
}
for (int i = 0; i <= 10; i++)//This keeps LED indicator LED pin on high until button is pressed
{ //then blinks in sync with rotation of motor until released
(analogRead(A0) / 4 + 10);
}
val1 = (analogRead(val1) * 4 + 2); //scale it to use it with the stepper mtr (value between 0 and 175)
} // 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(A0) / 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(A0) / 4 + 10);
i--;
if (i < 1) break;
{
lcd.setCursor(7, 0);
lcd.print(analogRead(i--) / 4 + 10);
}
digitalWrite(blue, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(A0) / 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(A0) / 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(A0) / 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(A0) / 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(A0) / 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(A0) / 4 + 10);
i--;
if (i < 1) break;
{
lcd.setCursor(12, 2);//print out the value of the pushbutton
lcd.print(i--);
}
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.