week3

This week's assignment is to program a microcontroller display to show the time and the state of a sensor attached to your controller. 

Because I moved to Shanghai this week, I did not order SSD1306 screens online, I went to the electronics market in Shanghai to try to find a similar one. Under the recommendation of the merchant, I only bought the ST7920 HF12864-16 LCD display. Unfortunately, I did not find a fully compliant datasheet. I tried to use the U8g2 library and control it in Arduino, but it didn’t succeed.Only the screen has light but no fonts appear. 

IMG_3049.HEIC

For the HID part, I try to use a pushbutton to replace the up arrow key.

Arduino code as follows:

/*
* try to replace UP_ARROW key with pushbutton

reference link:
https://github.com/tigoe/hid-examples/blob/main/KeyboardPushbutton/KeyboardPushbutton.ino
*/

#include <Keyboard.h>
// pushbutton on pin 2 is connected to ground:
const int buttonPin = 2;
// debounce delay for pushbutton:
const int debounceDelay = 4;
// previous state of the button:
int lastButtonState = HIGH;

void setup() {
  Serial.begin(9600);
  Keyboard.begin();
  // set button input mode:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // read button:
  int buttonState = digitalRead(buttonPin);
  // if it has changed:
  if (buttonState != lastButtonState) {
    // wait until button state stabilizes:
    delay(debounceDelay);
    // if it's low:
    if (buttonState == LOW) {
      // send keystroke:
      Keyboard.press(KEY_UP_ARROW);
     
    }else{
      Keyboard.release(KEY_UP_ARROW);

    }
    // save current state for next time:
    lastButtonState = buttonState;
  }
}