---
# System prepended metadata

title: Lab7 4 x 4 Keyboard with Step Motor
tags: [微算機]

---

# Lab7 4 x 4 Keyboard with Step Motor

## Members:
411085031 劉又瑄
410985004 陳文彥
411085020 林漢昕

## Principle: 
Controlling a stepp motor with an Arduino involves configuring the Arduino pins, defining the number of steps, writing the code, and using a stepper motor driver.

## Program:
```c=
##include <Keypad.h>
///////////////////5線式步進馬達設定//////////////////////////////////////////////////
#include <Stepper.h>
int steps[] = {14, 15, 16, 17}; // 步進馬達步數順序
int spd = 6;
///////////////////5線式步進馬達設定/////////////////////////////////////////////////
// keypad
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] =
    {{'0', '1', '2', '3'},
     {'4', '5', '6', '7'},
     {'8', '9', '@', '@'},
     {'@', '@', 'A', 'B'}};

byte rowPins[ROWS] = {4, 5, 6, 7};   // connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10, 11}; // connect to the column pinouts of the keypad
// initialize an instance of class NewKeypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char col[] = {47, 49, 51, 53};
char row[] = {38, 40, 42, 44, 46, 48, 50, 52};
char TAB[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
              0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};
void light(int index, int num)
{
    digitalWrite(col[index], LOW);
    for (int k = 0; k < 8; k++)
        digitalWrite(row[k], TAB[num] >> k & 0x01);

    delay(1);
    digitalWrite(col[index], HIGH);
}

void setNumber(int num)
{
    light(3, num % 10);
    light(2, num / 10 % 10);
    light(1, num / 100 % 10);
    light(0, num / 1000);
}

void setup()
{
    for (int i = 0; i < 4; i++)
    {
        pinMode(col[i], OUTPUT);
        digitalWrite(col[i], HIGH);
    }
    for (int j = 0; j < 8; j++)
    {
        pinMode(row[j], OUTPUT);
        digitalWrite(row[j], HIGH);
    }
    pinMode(steps[0], OUTPUT);
    pinMode(steps[1], OUTPUT);
    pinMode(steps[2], OUTPUT);
    pinMode(steps[3], OUTPUT);
}

bool isStop = false;
int nowStep = 0;
float nowFloor = 1, targetFloor = 1, lastTime = 0, moveFloor = 0;
int count = 0;
void goStair(bool isUp){
      digitalWrite(steps[nowStep % 4], LOW);
      nowStep += isUp ? 1 : -1;
      digitalWrite(steps[nowStep % 4], HIGH);
      delay(spd);
      count++;
}

void loop()
{
    setNumber(targetFloor * 100 + nowFloor);
    char customKey = customKeypad.getKey();
    if (customKey)
    {
        if (customKey >= '0' && customKey <= '9')
            targetFloor = customKey - '0';
        if (customKey == 'A') 
            isStop = true;
        if (customKey == 'B')
            isStop = false;
    }
    if (isStop)
        return;
    moveFloor = targetFloor - nowFloor;
    goStair(moveFloor > 0)
    if (count % 100 == 0)
    {
        if (nowFloor < targetFloor)
            nowFloor++;
        else if (nowFloor > targetFloor)
            nowFloor--;
        count = 0;
    }
}
```

## Description

### ``setup()``
Setup the input pins.

### ``setNumber()`` and ``light()``
In every frame, we will call ``setNumber()`` to set the number we want to show on LED, and the ``setNumber()`` will call ``light()`` to show each digit in number we give to ``setNumber()`` respectly.

### ``goStair()`` 
When the target stair is different to curr stair, this func will be call and cal the move of elevator.

### ``loop()``
Every loop, we will try to get user input, and call ``setNumber()`` to show curr floor.



## Result

[影片連結](https://drive.google.com/file/d/12gbwEck3qMISpd6Lmk8v-oR80lBLoqLf/view?usp=share_link)
