#include <Arduino.h>
#include <TinyDebug.h>
#include <digitalWriteFast.h>
constexpr byte Button {0};
constexpr byte LED1 {1};
constexpr byte LED2 {4};
constexpr byte BUTTON_ACTIVE_STATE {LOW};
unsigned int adc_value = 0;
byte COUNTER = 0;
byte ADC_SEL = 0;
// Pinlayout:
// https://github.com/SpenceKonde/ATTinyCore/blob/v2.0.0-devThis-is-the-head-submit-PRs-against-this/avr/extras/ATtiny_x5.png
byte ADCList[2] = {A3, A1}; // PB3 phys. pin 2 , PB2 phys. pin 7
byte LEDList[2] = {LED1, LED2};
void wait_button(byte, byte);
unsigned int adc_read(byte, unsigned int);
void Blink(byte, byte);
void waits(unsigned int);
void setup() {
Debug.begin();
Debug.println("Start...");
pinMode(LEDList[0], OUTPUT);
pinMode(LEDList[1], OUTPUT);
pinMode(Button, INPUT_PULLUP);
}
void loop() {
wait_button(Button, BUTTON_ACTIVE_STATE);
adc_value = adc_read(ADCList[ADC_SEL], 10);
COUNTER = map(adc_value, 0, 1023, 1, 10);
Debug.print("ADC gelesen: ");
Debug.println(adc_value);
Debug.print("Counter: ");
Debug.println(COUNTER);
Blink(LEDList[ADC_SEL], COUNTER);
wait_button(Button, false);
ADC_SEL = (ADC_SEL + 1) & 0b01; // Maximum index = 1
}
void wait_button(byte pin, byte active_state) {
byte button_state {not active_state};
while (button_state != active_state) {
button_state = (digitalReadFast(pin) != active_state) ? not active_state : active_state;
}
}
unsigned int adc_read(byte pin, unsigned int counts) {
Debug.print(" Channel: ");
Debug.println(pin - 6);
unsigned long value_sum = 0;
for (unsigned int i = 0; i < counts; ++i) { value_sum += analogRead(pin); }
return static_cast<unsigned int>(value_sum / counts);
}
void Blink(byte pin, byte counts) {
for (byte i = 0; i < counts; ++i) {
digitalWrite(pin, HIGH);
waits(250);
digitalWrite(pin, LOW);
waits(750);
}
}
void waits(unsigned int waittime) { delay(waittime); }