/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-i2c-scanner-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/
#include <Wire.h>
#include <U8g2lib.h>
#define OLED_RST -1
#define OLED_SCL 5
#define OLED_SDA 4
U8G2_SH1107_128X128_F_HW_I2C u8g2(U8G2_R0, OLED_SCL, OLED_SDA);
#define STOP 0
#define PLAY 1
#define FF 2
#define REW 3
const char * stateStr[] = {"STOP" ,"PLAY", "FF", "REW"};
const int nsectors = 6;
int pulses = 0;
int turns = 0;
int sector = 0;
int state = STOP;
double tapeSpeed = 15.0;
double tapeTime = 12.8;
double tapeTimeLeft = 17.2;
double outsideDiam = 4.2;
const byte playPin = 11;
const byte ffPin = 12;
const byte rewPin = 13;
const byte stopPin = 14;
// calculate time from tape length and speed and display it on the OLED
// currently with the standard arduino, it is not fast wenouh to do this display
// for every loop iteration, so on;y do this while stopped
void displayTimes() {
//Serial.print("8-");Serial.print(turns);Serial.print("/");Serial.println(sector);
// update global time
int mins = int(tapeTime / 60.0);
double secs = tapeTime - 60.0 * mins;
//Serial.print("4-");Serial.print(mins);Serial.print(":");Serial.println(secs);
u8g2.drawFrame(0, 64, 128,64);
u8g2.setCursor(6, 88);
u8g2.setFont(u8g2_font_t0_16_tr);
u8g2.print(F("Time:"));
u8g2.setFont(u8g2_font_t0_18_tr);
u8g2.setCursor(64, 88);
u8g2.print(tapeTime);
u8g2.setFont(u8g2_font_t0_12_tr );
int minsLeft = int(tapeTimeLeft / 60.0);
double secsLeft = tapeTimeLeft - 60.0 * minsLeft;
u8g2.setCursor(10, 112);
u8g2.print(String("Time Left:") + String(minsLeft) + String(":") + String(secsLeft, 1));
u8g2.setCursor(10, 124);
u8g2.print(F("Diameter: "));
u8g2.print(outsideDiam, 1);
}
void displayState() {
u8g2.setFont(u8g2_font_t0_12_tr);
u8g2.setCursor(6, 18);
u8g2.print(stateStr[state]);
u8g2.setFont(u8g2_font_inb27_mr);
u8g2.setCursor(10, 52);
u8g2.print(String(turns) + "/" + String(sector));
}
void doDisplay(int n) {
u8g2.clearBuffer();
displayState();
if (n == 2) {
//updateGlobals(turns, sector);
u8g2.drawFrame(0, 0, 128, 64);
displayTimes();
}
u8g2.sendBuffer();
}
// arduino's initialization function
void setup() {
Serial1.begin(115200);//enable serial monitor
delay(1000);
Serial1.println("STARTED");
u8g2.begin();
delay(1000); // wait for initializing
doDisplay(2);
pinMode(playPin, INPUT);
pinMode(ffPin, INPUT);
pinMode(rewPin, INPUT);
pinMode(stopPin, INPUT);
Serial1.println("SET UP");
}
int lastState = PLAY;
void setState() {
ulong t = millis();
int play = digitalRead(playPin);
int ff= digitalRead(ffPin);
int rew = digitalRead(rewPin);
int stop= digitalRead(stopPin);
//Serial1.print(String(play) + " " + String(ff) + " " + String(rew) + " " + String(stop) + " ");
if (stop == HIGH) {
// one of play/ff/rew is now low
if (play == LOW) {
state = PLAY;
} else if (ff == LOW){
state = FF;
} else if (rew == LOW){
state = REW;
} else {
state = STOP;
}
if (state != lastState) {
Serial1.print(millis() - t);Serial1.print(":");Serial1.println(stateStr[state]);
lastState = state;
}
} else {
if (lastState != STOP) {
Serial1.println("STOPPED");
lastState = state;
}
}
}
void loop() {
setState();
pulses += 1;
turns = pulses / nsectors;
sector = pulses % nsectors + 1;
doDisplay(2);
delay(100);
}
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
oled1:GND.1
oled1:VCC
oled1:SDA
oled1:SCL.1
chip1:VCC
chip1:GND
chip1:STOP
chip1:PLAY
chip1:FF
chip1:REW
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r