From: Ivan Stefanov Date: Tue, 17 Nov 2020 17:20:10 +0000 (+0200) Subject: Interrupt code examples X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=c3e95dc305785d6d2de4e82840568d795a6fb93f;p=vmks.git Interrupt code examples --- diff --git a/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-2.png b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-2.png new file mode 100644 index 0000000..ee86ee9 Binary files /dev/null and b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-2.png differ diff --git a/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-3.png b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-3.png new file mode 100644 index 0000000..670933f Binary files /dev/null and b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-3.png differ diff --git a/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-4.png b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-4.png new file mode 100644 index 0000000..cd5fede Binary files /dev/null and b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-4.png differ diff --git a/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram.png b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram.png new file mode 100644 index 0000000..74e2241 Binary files /dev/null and b/Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram.png differ diff --git a/Blink_LED_Low_Level/Blink_LED_Low_Level.ino b/Blink_LED_Low_Level/Blink_LED_Low_Level.ino new file mode 100644 index 0000000..96ba026 --- /dev/null +++ b/Blink_LED_Low_Level/Blink_LED_Low_Level.ino @@ -0,0 +1,12 @@ +void setup() { + // put your setup code here, to run once: + DDRB |= (1 << PB5); +} + +void loop() { + // put your main code here, to run repeatedly: + PORTB |= (1 << PB5); + _delay_ms(500); + PORTB &= ~(1 << PB5); + _delay_ms(500); +} diff --git a/LED_Strip_Moving_LED_-_Interrupt/LED Strip Moving LED - Interrupt Tinkercad.png b/LED_Strip_Moving_LED_-_Interrupt/LED Strip Moving LED - Interrupt Tinkercad.png new file mode 100644 index 0000000..1284fcc Binary files /dev/null and b/LED_Strip_Moving_LED_-_Interrupt/LED Strip Moving LED - Interrupt Tinkercad.png differ diff --git a/LED_Strip_Moving_LED_-_Interrupt/LED_Strip_Moving_LED_-_Interrupt.ino b/LED_Strip_Moving_LED_-_Interrupt/LED_Strip_Moving_LED_-_Interrupt.ino new file mode 100644 index 0000000..5fdcdb9 --- /dev/null +++ b/LED_Strip_Moving_LED_-_Interrupt/LED_Strip_Moving_LED_-_Interrupt.ino @@ -0,0 +1,45 @@ +#include +#define NUM_PIXELS 10 +#define PIN 5 +Adafruit_NeoPixel strip(NUM_PIXELS, PIN, NEO_GRB); + +#define BTN_LEFT 3 +#define BTN_RIGHT 2 + +volatile uint8_t position = 0; + +void L_button_pressed_ISR(); +void R_button_pressed_ISR(); + +void setup() { + pinMode(BTN_LEFT, INPUT_PULLUP); + pinMode(BTN_RIGHT, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(BTN_LEFT), L_button_pressed_ISR, FALLING); + attachInterrupt(digitalPinToInterrupt(BTN_RIGHT), R_button_pressed_ISR, RISING); + + pinMode(PIN, OUTPUT); + strip.begin(); +} + +void L_button_pressed_ISR() { + if (position > 0) position--; +} + +void R_button_pressed_ISR() { + if (position < (NUM_PIXELS - 1)) position++; +} + +void loop() { + for (int i = 0 ; i < NUM_PIXELS; i++) + { + if (i == position) + { + strip.setPixelColor(i, strip.Color(255, 0, 0)); + } + else + { + strip.setPixelColor(i, strip.Color(0, 0, 255)); + } + } + strip.show(); +} diff --git a/LED_Strip_Moving_LED_-_Polling/LED Strip Moving LED - Polling Tinkercad.png b/LED_Strip_Moving_LED_-_Polling/LED Strip Moving LED - Polling Tinkercad.png new file mode 100644 index 0000000..70f3c24 Binary files /dev/null and b/LED_Strip_Moving_LED_-_Polling/LED Strip Moving LED - Polling Tinkercad.png differ diff --git a/LED_Strip_Moving_LED_-_Polling/LED_Strip_Moving_LED_-_Polling.ino b/LED_Strip_Moving_LED_-_Polling/LED_Strip_Moving_LED_-_Polling.ino new file mode 100644 index 0000000..6b452fd --- /dev/null +++ b/LED_Strip_Moving_LED_-_Polling/LED_Strip_Moving_LED_-_Polling.ino @@ -0,0 +1,42 @@ +#include +#define NUM_PIXELS 10 +#define PIN 5 +Adafruit_NeoPixel strip(NUM_PIXELS, PIN, NEO_GRB); + +#define BTN_LEFT 3 +#define BTN_RIGHT 2 + +uint8_t position = 0; + +void setup() { + pinMode(BTN_LEFT, INPUT_PULLUP); + pinMode(BTN_RIGHT, INPUT_PULLUP); + + pinMode(PIN, OUTPUT); + strip.begin(); +} + +void loop() { + if (digitalRead(BTN_LEFT) == LOW) + { + if (position > 0) position--; + } + if (digitalRead(BTN_RIGHT) == LOW) + { + if (position < (NUM_PIXELS - 1)) position++; + } + delay(50); + + for (int i = 0 ; i < NUM_PIXELS; i++) + { + if (i == position) + { + strip.setPixelColor(i, strip.Color(255, 0, 0)); + } + else + { + strip.setPixelColor(i, strip.Color(0, 0, 255)); + } + } + strip.show(); +} diff --git a/UART-Send/UART-Send.ino b/UART-Send/UART-Send.ino new file mode 100644 index 0000000..d6b3048 --- /dev/null +++ b/UART-Send/UART-Send.ino @@ -0,0 +1,17 @@ +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); +} + +void loop() { + // put your main code here, to run repeatedly: + Serial.print('V'); + Serial.println('V'); + + Serial.write(0x56); + while(Serial.available() > 0) + { + Serial.read(); + } + delay(5000); +} diff --git a/external_interrupt/external_interrupt.ino b/external_interrupt/external_interrupt.ino index 1d8b71f..5ea877d 100644 --- a/external_interrupt/external_interrupt.ino +++ b/external_interrupt/external_interrupt.ino @@ -1,5 +1,5 @@ -#define BTN_PIN 2 -#define LED_PIN 13 +#define BTN_PIN 2 +#define LED_PIN 13 void button_pressed_ISR(void); void slow_computation(void); @@ -8,38 +8,40 @@ volatile uint8_t led_state = 0; void setup() { - pinMode(BTN_PIN, INPUT_PULLUP); - pinMode(LED_PIN, OUTPUT); - attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, FALLING); + pinMode(BTN_PIN, INPUT_PULLUP); + pinMode(LED_PIN, OUTPUT); + attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, FALLING); + //attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, RISING); + //attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, CHANGE); - Serial.begin(9600); + Serial.begin(9600); } void loop() { - slow_computation(); - - if (led_state) - Serial.println("LED in on."); - else - Serial.println("LED in off."); + slow_computation(); + + if (led_state) + Serial.println("LED in on."); + else + Serial.println("LED in off."); } void button_pressed_ISR(void) { - if (led_state) - { - led_state = 0; - digitalWrite(LED_PIN, LOW); - } - else - { - led_state = 1; - digitalWrite(LED_PIN, HIGH); - } + if (led_state) + { + led_state = 0; + digitalWrite(LED_PIN, LOW); + } + else + { + led_state = 1; + digitalWrite(LED_PIN, HIGH); + } } void slow_computation(void) { - delay(5000); -} \ No newline at end of file + delay(5000); +}