diff --git a/Sketches/sketch_sep20a-AverageValue/.vscode/arduino.json b/Sketches/sketch_sep20a-AverageValue/.vscode/arduino.json index 353b2e9..6554c6d 100644 --- a/Sketches/sketch_sep20a-AverageValue/.vscode/arduino.json +++ b/Sketches/sketch_sep20a-AverageValue/.vscode/arduino.json @@ -1,4 +1,5 @@ { "port": "COM7", - "board": "arduino:avr:uno" + "board": "arduino:avr:uno", + "output": "C:\\Temp" } \ No newline at end of file diff --git a/Sketches/sketch_sep27a-LEDButton/.vscode/arduino.json b/Sketches/sketch_sep27a-LEDButton/.vscode/arduino.json new file mode 100644 index 0000000..3f89244 --- /dev/null +++ b/Sketches/sketch_sep27a-LEDButton/.vscode/arduino.json @@ -0,0 +1,6 @@ +{ + "board": "arduino:avr:uno", + "port": "COM7", + "output": "C:\\Temp", + "sketch": "sketch_sep27a-LEDButton.ino" +} \ No newline at end of file diff --git a/Sketches/sketch_sep27a-LEDButton/.vscode/c_cpp_properties.json b/Sketches/sketch_sep27a-LEDButton/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..fd99c76 --- /dev/null +++ b/Sketches/sketch_sep27a-LEDButton/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "C:\\Users\\Alex\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\**", + "C:\\Users\\Alex\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\**" + ], + "forcedInclude": [ + "C:\\Users\\Alex\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\cores\\arduino\\Arduino.h" + ], + "intelliSenseMode": "msvc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/Sketches/sketch_sep27a-LEDButton/sketch_sep27a-LEDButton.ino b/Sketches/sketch_sep27a-LEDButton/sketch_sep27a-LEDButton.ino new file mode 100644 index 0000000..cda9fa9 --- /dev/null +++ b/Sketches/sketch_sep27a-LEDButton/sketch_sep27a-LEDButton.ino @@ -0,0 +1,48 @@ +int buttonPin = 2; +int ledPin = 13; + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(buttonPin, INPUT); + Serial.begin(9600); +} + +int buttonState = 0; +int lastButtonState = 0; +int ledState = 0; +void loop() +{ + buttonState = digitalRead(buttonPin); + + /** + * "Toggle"-funksjon med LED. + * + * Gjør i praksis følgende: + * - Sjekker om knappen er trykt ned og i tillegg har blitt sluppet tidligere (for å unngå blinking av LED). + * - Dette gjøres ved å sjekke "lastButtonState"-variabelet som oppdateres senere i koden. + * - Setter "ledState" til det omvendte (HIGH/LOW) av det det var tidligere. + * - Serial.print av "LED state" (status). + * - Setter LED til HIGH/LOW for å slå av/på. + */ + if (buttonState == HIGH && buttonState != lastButtonState) { + ledState = ledState == HIGH ? LOW : HIGH; + + Serial.print("LED state: "); + Serial.println(ledState == HIGH ? "HIGH" : "LOW"); + digitalWrite(ledPin, ledState); + } + + /** + * Serial.print() "button state" oppdateringer + * + * - Sjekker om buttonState er forskjellig fra lastButtonState. + * - Oppdaterer lastButtonState og logger til Serial-konsoll. + */ + if (buttonState != lastButtonState) { + Serial.print("Button state changed: "); + Serial.println(buttonState == HIGH ? "HIGH" : "LOW"); + + lastButtonState = buttonState; + } +}