ES ordening
This commit is contained in:
71
ES/ES assignment 5/ES 5/Assignment-A/main.cpp
Normal file
71
ES/ES assignment 5/ES 5/Assignment-A/main.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <Arduino.h>
|
||||
#define DEBOUNCE_DELAY 50
|
||||
#define BLINK_INTERVAL 500
|
||||
|
||||
unsigned long lastBlinkTime = 0;
|
||||
bool blinkState = false;
|
||||
|
||||
bool isPressed(uint8_t pinMask, volatile uint8_t* pinReg) {
|
||||
static unsigned long lastDebounceTime[8] = {0};
|
||||
static bool lastStableState[8] = {true};
|
||||
static bool lastReadState[8] = {true};
|
||||
|
||||
uint8_t pinIndex = 0;
|
||||
while ((pinMask >> pinIndex) != 1) pinIndex++; // bepaalt welk bit van toepassing is
|
||||
|
||||
bool reading = (*pinReg & pinMask); // leest huidige status van de pin
|
||||
|
||||
if (reading != lastReadState[pinIndex]) {
|
||||
lastDebounceTime[pinIndex] = millis(); // bij verandering: debounce timer starten
|
||||
lastReadState[pinIndex] = reading;
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime[pinIndex]) > DEBOUNCE_DELAY) {
|
||||
lastStableState[pinIndex] = reading; // status pas bijwerken na stabiele periode
|
||||
}
|
||||
|
||||
return lastStableState[pinIndex];
|
||||
}
|
||||
|
||||
void setup() {
|
||||
DDRD |= _BV(DDD5) | _BV(DDD6);
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Knopstatussen uitlezen met debounce
|
||||
bool Button1Pressed = isPressed(_BV(PINB2), &PINB); // D10
|
||||
bool Button2Pressed = isPressed(_BV(PINB3), &PINB); // D11
|
||||
|
||||
if (Button1Pressed && !Button2Pressed) {
|
||||
// Alleen Button1 ingedrukt: LED D5 aan, D6 uit
|
||||
PORTD |= _BV(PORTD5);
|
||||
PORTD &= ~_BV(PORTD6);
|
||||
} else if (!Button1Pressed && Button2Pressed) {
|
||||
// Alleen Button2 ingedrukt: LED D6 aan, D5 uit en "Hello World!" printen
|
||||
PORTD &= ~_BV(PORTD5);
|
||||
PORTD |= _BV(PORTD6);
|
||||
static unsigned long lastHelloTime = 0;
|
||||
if (millis() - lastHelloTime > 100) {
|
||||
Serial.println("Hello World!\n");
|
||||
lastHelloTime = millis();
|
||||
}
|
||||
} else if (Button1Pressed && Button2Pressed) {
|
||||
// Beide knoppen ingedrukt: LEDs knipperen om en om
|
||||
if (millis() - lastBlinkTime >= BLINK_INTERVAL) {
|
||||
blinkState = !blinkState;
|
||||
lastBlinkTime = millis();
|
||||
}
|
||||
|
||||
if (blinkState) {
|
||||
PORTD |= _BV(PORTD5);
|
||||
PORTD &= ~_BV(PORTD6);
|
||||
} else {
|
||||
PORTD &= ~_BV(PORTD5);
|
||||
PORTD |= _BV(PORTD6);
|
||||
}
|
||||
} else {
|
||||
// Geen knop ingedrukt: beide LEDs uit
|
||||
PORTD &= ~(_BV(PORTD5) | _BV(PORTD6));
|
||||
}
|
||||
}
|
||||
88
ES/ES assignment 5/ES 5/Assignment-B/main.cpp
Normal file
88
ES/ES assignment 5/ES 5/Assignment-B/main.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define BLINK_INTERVAL 500
|
||||
#define DEBOUNCE_DELAY 50
|
||||
|
||||
volatile bool Button1 = false;
|
||||
volatile bool Button2 = false;
|
||||
|
||||
volatile unsigned long lastDebounceTimeButton1 = 0;
|
||||
volatile unsigned long lastDebounceTimeButton2 = 0;
|
||||
|
||||
bool lastButton1State = true;
|
||||
bool lastButton2State = true;
|
||||
|
||||
unsigned long lastBlinkTime = 0;
|
||||
bool blinkState = false;
|
||||
|
||||
void setup() {
|
||||
// LEDs op D5 en D6 als output
|
||||
DDRD |= _BV(DDD5) | _BV(DDD6);
|
||||
|
||||
// Knoppen D10 (PB2) en D11 (PB3) als input met pull-up
|
||||
DDRB &= ~(_BV(DDB2) | _BV(DDB3));
|
||||
PORTB |= _BV(PORTB2) | _BV(PORTB3);
|
||||
|
||||
// Pin Change Interrupts activeren voor PB2 en PB3
|
||||
PCICR |= _BV(PCIE0); // Enable PCINT[7:0] (PORTB)
|
||||
PCMSK0 |= _BV(PCINT2) | _BV(PCINT3);
|
||||
|
||||
sei();
|
||||
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
unsigned long now = millis();
|
||||
|
||||
// Lees huidige toestand
|
||||
bool currentButton1 = PINB & _BV(PINB2);
|
||||
bool currentButton2 = PINB & _BV(PINB3);
|
||||
|
||||
// Detecteer falling edge voor button1 (hoog -> laag)
|
||||
if (lastButton1State && !currentButton1 && (now - lastDebounceTimeButton1 > DEBOUNCE_DELAY)) {
|
||||
Button1 = !Button1; // toggle status
|
||||
lastDebounceTimeButton1 = now;
|
||||
}
|
||||
|
||||
// Detecteer falling edge voor button 2
|
||||
if (lastButton2State && !currentButton2 && (now - lastDebounceTimeButton2 > DEBOUNCE_DELAY)) {
|
||||
Button2 = !Button2; // toggle status
|
||||
lastDebounceTimeButton2 = now;
|
||||
}
|
||||
|
||||
// Update vorige staat
|
||||
lastButton1State = currentButton1;
|
||||
lastButton2State = currentButton2;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Button1 && !Button2) {
|
||||
PORTD |= _BV(PORTD5);
|
||||
PORTD &= ~_BV(PORTD6);
|
||||
} else if (!Button1 && Button2) {
|
||||
PORTD &= ~_BV(PORTD5);
|
||||
PORTD |= _BV(PORTD6);
|
||||
|
||||
static unsigned long lastHelloTime = 0;
|
||||
if (millis() - lastHelloTime > 100) {
|
||||
Serial.println("Hello World!");
|
||||
lastHelloTime = millis();
|
||||
}
|
||||
} else if (Button1 && Button2) {
|
||||
if (millis() - lastBlinkTime >= BLINK_INTERVAL) {
|
||||
blinkState = !blinkState;
|
||||
lastBlinkTime = millis();
|
||||
}
|
||||
|
||||
if (blinkState) {
|
||||
PORTD |= _BV(PORTD5);
|
||||
PORTD &= ~_BV(PORTD6);
|
||||
} else {
|
||||
PORTD &= ~_BV(PORTD5);
|
||||
PORTD |= _BV(PORTD6);
|
||||
}
|
||||
} else {
|
||||
PORTD &= ~(_BV(PORTD5) | _BV(PORTD6));
|
||||
}
|
||||
}
|
||||
101
ES/ES assignment 5/ES 5/Assignment-C/main.cpp
Normal file
101
ES/ES assignment 5/ES 5/Assignment-C/main.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define LED0 PORTD5 // D5
|
||||
#define LED1 PORTD6 // D6
|
||||
#define LED2 PORTB0 // D8
|
||||
#define LED3 PORTB1 // D9
|
||||
#define BUILTIN_LED PORTB5 // D13
|
||||
|
||||
#define BUTTON_SLOW PINB2 // D10
|
||||
#define BUTTON_FAST PINB3 // D11
|
||||
|
||||
#define COMPARE_MIN 1000
|
||||
#define COMPARE_MAX 50000
|
||||
|
||||
volatile unsigned char counter = 0;
|
||||
volatile unsigned int compareValue = COMPARE_MAX;
|
||||
|
||||
void setupIO() {
|
||||
// LEDs output
|
||||
DDRD |= (1 << LED0) | (1 << LED1);
|
||||
DDRB |= (1 << LED2) | (1 << LED3) | (1 << BUILTIN_LED);
|
||||
|
||||
// Buttons input with pull-up
|
||||
DDRB &= ~((1 << BUTTON_SLOW) | (1 << BUTTON_FAST));
|
||||
PORTB |= (1 << BUTTON_SLOW) | (1 << BUTTON_FAST);
|
||||
|
||||
// Pin change interrupt for D10/D11
|
||||
PCICR |= (1 << PCIE0);
|
||||
PCMSK0 |= (1 << PCINT2) | (1 << PCINT3);
|
||||
}
|
||||
|
||||
// Setup Timer1 with COMPA (counter) and COMPB (1Hz LED)
|
||||
void setupTimer1() {
|
||||
TCCR1A = 0;
|
||||
TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // CTC, prescaler 64
|
||||
|
||||
OCR1A = compareValue; // 4-bit counter update speed
|
||||
OCR1B = 249999; // 1 Hz = (16e6 / 64) / 250000
|
||||
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B);
|
||||
}
|
||||
|
||||
// Show counter value on 4 LEDs
|
||||
void updateLEDs(unsigned char val) {
|
||||
if (val & 0x01) PORTD |= (1 << LED0); else PORTD &= ~(1 << LED0);
|
||||
if (val & 0x02) PORTD |= (1 << LED1); else PORTD &= ~(1 << LED1);
|
||||
if (val & 0x04) PORTB |= (1 << LED2); else PORTB &= ~(1 << LED2);
|
||||
if (val & 0x08) PORTB |= (1 << LED3); else PORTB &= ~(1 << LED3);
|
||||
}
|
||||
|
||||
// Counter interrupt (frequency adjustable)
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
counter = (counter + 1) & 0x0F;
|
||||
updateLEDs(counter);
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPB_vect) {
|
||||
PINB |= (1 << BUILTIN_LED); // Toggle built-in LED
|
||||
}
|
||||
|
||||
// Button interrupt: handle speed control
|
||||
ISR(PCINT0_vect) {
|
||||
static unsigned char lastState = 0xFF;
|
||||
unsigned char current = PINB;
|
||||
|
||||
// Falling edge: slow down
|
||||
if (!(current & (1 << BUTTON_SLOW)) && (lastState & (1 << BUTTON_SLOW))) {
|
||||
if (compareValue < COMPARE_MAX) {
|
||||
compareValue <<= 1;
|
||||
if (compareValue > COMPARE_MAX) compareValue = COMPARE_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
// Falling edge: speed up
|
||||
if (!(current & (1 << BUTTON_FAST)) && (lastState & (1 << BUTTON_FAST))) {
|
||||
if (compareValue > COMPARE_MIN) {
|
||||
compareValue >>= 1;
|
||||
if (compareValue < COMPARE_MIN) compareValue = COMPARE_MIN;
|
||||
}
|
||||
}
|
||||
|
||||
lastState = current;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
cli();
|
||||
setupIO();
|
||||
setupTimer1();
|
||||
sei();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static unsigned int lastValue = 0;
|
||||
cli();
|
||||
if (compareValue != lastValue) {
|
||||
TIMSK1 &= ~(1 << OCIE1A);
|
||||
OCR1A = compareValue;
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
lastValue = compareValue;
|
||||
}
|
||||
sei();
|
||||
}
|
||||
BIN
ES/ES assignment 5/ES 5/ES assignment 5.docx
Normal file
BIN
ES/ES assignment 5/ES 5/ES assignment 5.docx
Normal file
Binary file not shown.
Reference in New Issue
Block a user