#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#define BUTPIN PB3
#define DATAPIN PB1
uint8_t width = 128;
uint8_t height = 32;
uint8_t menlen = 4;
bool but = false;
int cnt = 0;
uint16_t adc_value;
int8_t curmen = 0;
String option;
int8_t rpts = 50;
void updateDisplay() {
// Clear whatever random data has been left in memory.
oled.clear();
oled.print(F("MENU: "));
if (but) oled.println(F("SENDING"));
else oled.println(F(" "));
oled.println(option);
}
void setup() {
// put your setup code here, to run once:
pinMode(BUTPIN, INPUT_PULLUP);
pinMode(DATAPIN, OUTPUT);
oled.begin(width, height, sizeof(tiny4koled_init_128x32br), tiny4koled_init_128x32br);
oled.setFont(FONT8X16);
// Setup the first half of memory.
updateDisplay();
// Switch the half of RAM that we are writing to, to be the half that is non currently displayed.
oled.switchRenderFrame();
// Setup the second half of memory.
updateDisplay();
// Switch back to being ready to render on the first frame while displaying the second frame.
oled.switchFrame();
// Turn on the display.
oled.on();
// ADC Left Shift Result
ADMUX |= 1<<ADLAR;
// ADC Voltage Referrence
ADMUX |= 0<<REFS2|0<<REFS1|0<<REFS0; // Vcc is used as Voltage Referrence
// ADC Input Channel/s
ADMUX |= 0<<MUX3|0<<MUX2|1<<MUX1|0<<MUX0; // Use ADC1 on pin PB4 as ADC input
// ADC clock prescaler
ADCSRA |= 0<<ADPS2|1<<ADPS1|1<<ADPS0; // Use a divide by 8 Prescaler.
// Sysclock = 8MHz, ADCclk = 1MHz (max)
ADCSRA |= 1<<ADEN; // Enable the ADC
}
void loop() {
//delay(50);
updateDisplay();
oled.switchFrame();
ADCSRA |= 1<<ADSC; // Start the ADC conversion // a 0 on ADCSRA means conversion is complete
adc_value = ADCL|(ADCH << 8); // ADC value is left justified. Use operand precedence rules
// left to right precedence, so ADCL is read first
curmen = map(adc_value, 0, 65472, 0, menlen);
switch (curmen)
{
case 0:
option = "FIRST OPTION ";
break;
case 1:
option = "SECOND OPTION ";
break;
case 2:
option = "THIRD OPTION ";
break;
case 3:
option = "FOURTH OPTION ";
break;
default:
break;
}
if (digitalRead(BUTPIN) == LOW){
if (!but){
for (int i = 0; i < rpts; i++){
digitalWrite(DATAPIN, HIGH);
delay(curmen+1);
digitalWrite(DATAPIN, LOW);
delay(curmen+1);
option = "SENT";
}
but = true;
cnt = 0;
}
}
if (but && cnt > 5){
but = false;
}
cnt++;
delay(50);
}