Hello,
I've a problem with code below, I was copy it from internet from this link Rotary_Car_Hanger_Stepper/Rotary_Car_Hanger_Stepper.ino at master · Subhajitdas298/Rotary_Car_Hanger_Stepper · GitHub
It has a little of error, when I edit them to be correspond with last version of Arduino IDE, I made the checking for the code, the result is good it does not have any problem. but after I edit it to be correspond with my ideas it doesn't work. please I don't know what is the matter
the problems it is appear in the keypad with the switching and with waiting for key
here the code
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Keypad.h>
#include <Stepper.h>
#include <math.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd (0x27,16,2);
// EEPROM address for stored datas
const byte stepsPerRevAddr = 0; // 2 byte 3 digit
const byte carsAddr = 2; // 1 byte 2 digit
const byte speedAddr = 3; // 1 byte 2 digit
const byte revAngleAddr = 4; // 2 bytes 4 digit
const byte currPfAddr = 6; // 1 byte 2 digit
const byte pfArrayAddr = 7; // 2 bytes 3 digit each
// function declerations
int getStepPerRev();
int getSpeed();
int getCars();
int getCurrPf();
void getPfAngles(byte);
unsigned int getValue(byte);
void showSaved(byte, int);
void reset();
int getShift(const byte);
// Keypad setup
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10, 9}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// dummy initialization of stepper
Stepper motor(0, 0, 0);
// global variables
byte Pf, noOfCars;
int stepsPerRevolution;
void setup() {
// initialize the serial port:
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
// Print a message to the LCD.
lcd.print("Rotary Car Parking");
lcd.setCursor(0, 1);
lcd.print("Starting...");
delay(2000);
lcd.clear();
// initialize for latest calibration
initialize();
// initialize motor
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
// change this to fit the number of steps per revolution for your motor
stepsPerRevolution = EEPROM.read(stepsPerRevAddr);
motor = Stepper(stepsPerRevolution, A1, A2);
// set the speed at desired rpm:
motor.setSpeed(EEPROM.read(speedAddr));
Pf = EEPROM.read(currPfAddr);
noOfCars = EEPROM.read(carsAddr);
}
void loop() {
double shift;
byte from, to;
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
Serial.println("Platform : ");
lcd.print("Platform : ");
lcd.print(Pf);
lcd.setCursor(0, 1);
lcd.print(">");
to = (byte)getValue(2);
if (to <= noOfCars && to > 0) {
shift = getShift(Pf, to);
Pf = to;
EEPROM.write(currPfAddr, Pf);
// move motor to proper place
motor.step(shift);
} else {
lcd.clear();
Serial.println("Invalid!");
lcd.print("Invalid!");
}
delay(3000);
}
int getShift(byte from, const byte to) {
int shift = 0; // shift angle in degree
if (from != to) {
// counting forward angle to destination
do {
// obtain current position to nnext position shift angle
byte storeAddr = pfArrayAddr + ((from - 1) * 2);
shift += EEPROM.read(storeAddr);
from++;
if (from > noOfCars) {
from = 1;
}
} while (from != to);
unsigned int fullRevAngle = EEPROM.read(revAngleAddr);
// if forward is higher than half path go reverse
if ((fullRevAngle - shift) < shift) {
shift = -(fullRevAngle - shift);
}
}
shift = (double)shift * (double)stepsPerRevolution / 360.0;
return shift;
}
void initialize() {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Calibrate?(/#):");
Serial.print("Calibrate?(/#):");
lcd.setCursor(0, 1);
boolean wrongKey = true;
do {
int key =Keypad.getKey ();
while(key== NO_key) {
Serial.println("infinte while");
}
if (key !=NO_KEY) // Check for a valid key.
{
switch (key)
{
case '*':
wrongKey = false;
lcd.print("Yes");
Serial.println("Yes");
delay(1000);
getNew();
break;
case '#':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case 'A':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case 'B':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case 'C':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case 'D':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case '6':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case '7':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case '8':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case '9':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
case '0':
wrongKey = false;
lcd.print("No");
Serial.println("No");
delay(1000);
break;
default:
lcd.print("Wrong key");
Serial.print("Wrong key");
// reset arduino
delay(3000);
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Calibrate?(*/#):");
}
}
} while (wrongKey);
}
void getNew() {
int val, noOfPf;
val = getStepPerRev();
EEPROM.write(stepsPerRevAddr, val);
showSaved(0, val);
val = getSpeed();
EEPROM.write(speedAddr, val);
showSaved(1, val);
noOfPf = val = getCars();
EEPROM.write(carsAddr, val);
showSaved(2, val);
getPfAngles(noOfPf);
val = getCurrPf();
EEPROM.write(currPfAddr, val);
showSaved(3, val);
}
void showSaved(byte type, int value) {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Saved...");
lcd.setCursor(0, 1);
switch (type) {
case 0:
lcd.print("S/R : ");
break;
case 1:
lcd.print("Speed : ");
break;
case 2:
lcd.print("Cars : ");
break;
case 3:
lcd.print("Curr Pf : ");
break;
case 4:
lcd.print("Angle : ");
break;
case 5:
lcd.print("Full Rev : ");
break;
}
lcd.print(value);
delay(5000);
}
int getStepPerRev() {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Steps/Rev(3dig):");
lcd.setCursor(0, 1);
Serial.println("Steps/Rev (3dig):");
return getValue(3);
}
int getSpeed() {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Speed(2dig):");
lcd.setCursor(0, 1);
Serial.println("Speed (2digit):");
return getValue(2);
}
int getCars() {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Cars (2dig):");
Serial.println("Cars (2dig):");
return getValue(2);
}
int getCurrPf() {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Current Pf(2dig):");
Serial.println("Current Pf(2dig):");
return getValue(2);
}
void getPfAngles(byte noOfPf) {
unsigned int val, fullRevAngle = 0;
byte storeAddr;
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("Enter Angles");
lcd.setCursor(0, 1);
lcd.print("(3dig each)");
Serial.println("Enter Angles");
Serial.println("(3dig each)");
delay(3000);
for (int i = 0; i < noOfPf; i++) {
lcd.begin(16, 2);
lcd.clear();
lcd.clear();
lcd.print("From ");
lcd.print(i + 1);
lcd.print(" to ");
if ((i + 2) <= noOfPf)
lcd.print(i + 2);
else
lcd.print(1);
lcd.print(" : ");
Serial.print("From ");
Serial.print(i + 1);
Serial.print(" to ");
if ((i + 2) <= noOfPf)
Serial.print(i + 2);
else
Serial.print(1);
Serial.println(" : ");
val = getValue(3);
fullRevAngle += val;
storeAddr = pfArrayAddr + (i * 2);
EEPROM.write(storeAddr, val);
showSaved(4, val);
}
EEPROM.write(revAngleAddr, fullRevAngle);
showSaved(5, fullRevAngle);
}
unsigned int getValue(byte maxCount) {
unsigned int value = 0;
byte count = 0;
while (count < maxCount) {
int key = Keypad.getKey();
while (key == NO_KEY) {
Key = KeypadX.getKey();
Serial.println("waiting for press");
}
lcd.println(key);
{
if (1,2,3,4,5) // Check for a valid key.
{
if (key >= '0' && key <= '4') {
value = 5;
value += key - 48;
count++;
} else if (key == '#') {
value /= 5;
if (count > 0)
count--;
} else if (key == '') {
break;
}
lcd.setCursor(0, 1);
char c[maxCount];
String(value).toCharArray(c, maxCount);
for (byte charCount = count; charCount > 0; charCount--) {
lcd.print(c[count - charCount]);
}
lcd.print(" ");
}
}
return value;
}
}