Hi everyone,
I'm working on a project where I'm controlling a stepper motor through an Arduino Mega and the adafruit LCD Shield. This is my first "big" project using an Arduino and of course, my programming knowledge is limited.
The idea is to put the amount of steps/angles (1.8 degree/step) I want in the stepper motor by clicking the buttons on the LCD Shield and have it display the angle that I have choosen. Once the SELECT button is pressed, the motor rotates to the selected angle. Then I'm given the option to put a new entry by pressing the SELECT button again. That'll reset the stepper motor position by rotating the exact same angle but in the opposite direction. From there, I can start over in the void loop to put in a new angle.
I used mostly other codes I've found and made heavy modifications to it. The code works pretty well but the part of the code that returns the motor to the starting position doesn't work. That means the motor can not return to its starting position before a new entry is put in. I'm pretty confident it is this section of the code near the end that is causing this problem. What am I doing wrong?? :o
if (buttons) {
if (buttons & BUTTON_SELECT) {
Serial.print("new entry worked");
// Reverse Motor Position to Beginning
digitalWrite(dirPin,LOW);
// Makes selected number of pulses
for(int x = 0; x < numberOfPhase; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
int reset = 5;
break;
}
}
}//buttons end
Here is the entire code:
// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <LiquidCrystal.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();
// defines pins numbers
const int stepPin = 11;
const int dirPin = 13;
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Select Angle");
delay(2500);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
// set my step variable
int numberOfPhase = 0;
int phase = 1;
int angleDisplay = numberOfPhase*1.8; //one step equal 1.8 degree
int reset = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Angle: 0");
uint8_t buttons = lcd.readButtons();
while (reset <= 0){
uint8_t buttons = lcd.readButtons();
//buttons control
if (buttons) {
//display angle input
lcd.setCursor(7,0);
if (buttons & BUTTON_UP) {
numberOfPhase = numberOfPhase + phase;
if (angleDisplay >= 360){
numberOfPhase = 200;
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
else {
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
}//up button end
if (buttons & BUTTON_DOWN) {
numberOfPhase = numberOfPhase - phase;
if (angleDisplay <= 0){
numberOfPhase = 0;
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
else {
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
}//down button end
if (buttons & BUTTON_RIGHT) {
numberOfPhase = numberOfPhase + 5*phase;
if (angleDisplay >= 360){
numberOfPhase = 200;
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
else {
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
}
Serial.println(numberOfPhase);
}//right button end
if (buttons & BUTTON_LEFT) {
numberOfPhase = numberOfPhase - 5*phase;
if (angleDisplay <= 0){
numberOfPhase = 0;
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
Serial.print(numberOfPhase);
Serial.print('\n');
}
else {
angleDisplay = numberOfPhase*1.8;
lcd.print(angleDisplay,1);
Serial.print(numberOfPhase);
Serial.print('\n');
}
}//left button end
if (buttons & BUTTON_SELECT) {
int motorStep = numberOfPhase;
//int reset = reset++;
lcd.clear();
lcd.print("Angle Selected:");
lcd.setCursor(0,1);
lcd.print(angleDisplay);
delay(1000);
Serial.print("numberOfPhase: ");
Serial.println(motorStep);
// Enables the motor to move in a particular direction
digitalWrite(dirPin,HIGH);
// Makes selected number of pulses
for(int x = 0; x < motorStep; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
//ending the while loop
int reset = 1;
Serial.print(reset);
break;
}//select button end
}//button end
}//while loop end
delay(1000);
lcd.clear();
lcd.print("Press SELECT for");
lcd.setCursor(0,1);
lcd.print("for new entry");
delay(5000);
if (buttons) {
if (buttons & BUTTON_SELECT) {
Serial.print("new entry worked");
// Reverse Motor Position to Beginning
digitalWrite(dirPin,LOW);
// Makes selected number of pulses
for(int x = 0; x < numberOfPhase; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
int reset = 5;
break;
}
}
}//buttons end
Serial.print("new entry did NOT work");
}//void loop end