Project of smart parking

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;
}
}

Welcome

Please use Edit -> Copy for Forum in the IDE. Next edit your post and replace all code by the code that you copied. Next save the post. That way we can be sure that what we see is indeed your code.

This e.g. seems to have the wrong ticks

char hexaKeys[ROWS][COLS] = { {‘1’,‘2’,‘3’,‘A’}, {‘4’,‘5’,‘6’,‘B’}, {‘7’,‘8’,‘9’,‘C’}, {’*’,‘0’,’#’,‘D’} };

"It doesn't work" does not mean much. Tell us what you expect it to do and what it actually does.

Whatever you intend here is probably not what it does…

1,2,3,4,5 is just a complicated way to say “true”.

So what do you want to do there?

a7

Some more mistakes by the looks of it

  1. count–;
  2. lcd.begin(16, 2) in various places
  3. variable customKeypad never used

And a question: did you ever edit your code in a word processor (like MS Word or OpenOffice Writer)?

[quote="sterretje, post:2, topic:851836"]
Welcome

Please use Edit → Copy for Forum in the IDE. Next edit your post and replace all code by the code

it is now be okay?

#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;
}
}

I have a design for my project you can see the shape of it here at this link

https://drive.google.com/drive/folders/1_3L7gOe-ffGClRcZmR_BpqbYze5wUimH?usp=sharing

the Idea is when I pressed on the number of keypad the stepper motor should run and it should by move the plate which it has a car to up then it should be stoped, it seems as lift to work.

so, the code it has probles with keypad and in LCD I think or with the mathematics functions I don,t konw what is the matter exactly

I want to press these keys in the keypad, because my design it has only five plates

Do you mean, it should be as this?

LiquidCrystal_I2C lcd (0x27,16,2);

or as this

LiquidCrystal_I2C lcd (0x3f,16,2);

Can you tell me what is the write one then

if it be as this, if (key)

can it be work. because I tried these cases, but the proble is same

No I don't use any of them (words)

Thanks for the corrected version of the sketch. Let's go through the errors that I get when I compile your sketch; there is also a relevant warning (at the end). Note that my linenumbers are not the same but you can find them yourself when you compile the code :wink:

sketch_apr21a:147: error: expected primary-expression before '.' token
     int key = Keypad.getKey ();
                     ^

Keypad is a class, not an object. In Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); you declare an object customKeypad and that is what you must use. There are a few places where this error occurs.

sketch_apr21a:148: error: 'NO_key' was not declared in this scope
     while (key == NO_key) {
                   ^

I already mentined that C/C++ is case sensitive. The compiler does not know about NO_key because it is called NO_KEY. You seem to have it correct in other places.

sketch_apr21a:385: error: expected unqualified-id before '=' token
       Key = KeypadX.getKey();
           ^

There is no variable called Key; remember. C/C++ is case sensitive so the variable is called key. There is also no variable/object KeypadX; just a wild guess that that also needs to be customKeypad.

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_69674\sketch_apr21a.ino:391:14: warning: left operand of comma operator has no effect [-Wunused-value]
       if (1, 2, 3, 4, 5) // Check for a valid key.
              ^

Read up how if statements work; you have it right a few lines later. You will also have to look at your logic there.

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_69674\sketch_apr21a.ino:416:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

Although just a warning, it indicates a flaw in your code. The reason for this is here

        lcd.print("     ");
      }
    }
    return value;
  }

The return must move to after the last shown }. Your function will currently return a random value if count is greater or equal to maxCount.

Advise:
It loos like you made various changes and ended up with this mess. Make one change at a time and compile; fix the errors (if any) before you make the next change.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.