]> kolegite.com Git - vmks.git/commitdiff
Interrupt code examples
authorIvan Stefanov <istefanov@elsys-bg.org>
Tue, 17 Nov 2020 17:20:10 +0000 (19:20 +0200)
committerIvan Stefanov <istefanov@elsys-bg.org>
Tue, 17 Nov 2020 17:20:10 +0000 (19:20 +0200)
Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-2.png [new file with mode: 0644]
Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-3.png [new file with mode: 0644]
Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram-4.png [new file with mode: 0644]
Blink_LED_Low_Level/Arduino-Uno-Pin-Diagram.png [new file with mode: 0644]
Blink_LED_Low_Level/Blink_LED_Low_Level.ino [new file with mode: 0644]
LED_Strip_Moving_LED_-_Interrupt/LED Strip Moving LED - Interrupt Tinkercad.png [new file with mode: 0644]
LED_Strip_Moving_LED_-_Interrupt/LED_Strip_Moving_LED_-_Interrupt.ino [new file with mode: 0644]
LED_Strip_Moving_LED_-_Polling/LED Strip Moving LED - Polling Tinkercad.png [new file with mode: 0644]
LED_Strip_Moving_LED_-_Polling/LED_Strip_Moving_LED_-_Polling.ino [new file with mode: 0644]
UART-Send/UART-Send.ino [new file with mode: 0644]
external_interrupt/external_interrupt.ino

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 (file)
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 (file)
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 (file)
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 (file)
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 (file)
index 0000000..96ba026
--- /dev/null
@@ -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 (file)
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 (file)
index 0000000..5fdcdb9
--- /dev/null
@@ -0,0 +1,45 @@
+#include <Adafruit_NeoPixel.h>
+#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 (file)
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 (file)
index 0000000..6b452fd
--- /dev/null
@@ -0,0 +1,42 @@
+#include <Adafruit_NeoPixel.h>
+#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 (file)
index 0000000..d6b3048
--- /dev/null
@@ -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);
+}
index 1d8b71fd3e515dfa536c0b56a699a7efa2f1846c..5ea877ddefc0ad56a46acb99dc084bc4ec0445f8 100644 (file)
@@ -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);
+}