// STM32 Nucleo-C031C6 I2C Example
// Simulation: https://wokwi.com/projects/365421666018061313
#include "LiquidCrystal_I2C.h"
#include <Keypad.h>
#include <Stepper.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { A1, A2, A3, A4 };
uint8_t rowPins[ROWS] = { 3, 4, 5, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int stepsPerRevolution = 360;
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);
int cur_floor = 1;
int tar_floor = 0;
void setup() {
Serial.begin(115200);
Serial.println("Hello, STM32!");
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("target:");
lcd.setCursor(0,1);
lcd.print("current:");
myStepper.setSpeed(60);
}
void move() {
if (tar_floor > cur_floor) {
while (tar_floor > cur_floor) {
myStepper.step(stepsPerRevolution);
lcd.setCursor(10,1);
cur_floor++;
lcd.print(cur_floor);
delay(500);
}
}
else if (tar_floor < cur_floor) {
while (tar_floor < cur_floor) {
myStepper.step(-stepsPerRevolution);
lcd.setCursor(10,1);
cur_floor--;
lcd.print(cur_floor);
delay(500);
}
}
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
tar_floor = key - '0';
lcd.setCursor(10,0);
lcd.print(tar_floor);
lcd.setCursor(10,1);
lcd.print(cur_floor);
move();
}
}