#include "myLib.h"
#define RSTBTN = 3
int POTPIN = A0
int BTNPIN = 5
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(POTPIN, INPUT);
pinMode(BTNPIN, INPUT_PULLUP);
void loop() {
//doSomething
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(POTPIN, INPUT);
pinMode(BTNPIN, INPUT_PULLUP);
void loop() {
//doSomething
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
void setup() {
pinMode(pushButton, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
digitalWrite(LED_BUILTIN, buttonState);
}
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
int potPin = A0
int ledPin = 6
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = map(analogRead(potPin), 0, 1024, 0, 255);
Serial.println(sensorValue);
digitalWrite(ledPin,sensorValue)
delay(500);
}
A | B | C |
0 | 1 | 0 |
1 | 1 | 1 |
X | 0 | Z |
void setup()
{
pinMode(pinNumber, INPUT_PULLUP);
};
Er zijn verschillende analoge operaties maar voor de moment is alleen ADC operatie belangrijk.
int ledPin = 9; // LED connected to digital pin 9
void setup() {
}
void loop() {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
}
const int ledPin = 13;
int ledState = LOW;
const long interval = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
delay(interval)
ledstate = !ledstate
digitalWrite(ledPin, ledState);
}
const int ledPin = 13;
int ledState = LOW;
long previousMillis = 0;
const long interval = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis > interval + previousMillis ) {
previousMillis = currentMillis;
ledstate = !ledstate
digitalWrite(ledPin, ledState);
}
}
Polling | Interrupts |
Synchroon | Asynchroon |
Niet dringend | Dringend |
Frequent | Sporadisch |
Board | Digital Pins Usable For Interrupts |
Uno, Nano, Mini, other 328-based | 2, 3 |
Mega, Mega2560, MegaADK | 2, 3, 18, 19, 20, 21 |
Micro, Leonardo, other 32u4-based | 0, 1, 2, 3, 7 |
Zero | all digital pins, except 4 |
MKR1000 | 0, 1, 4, 5, 6, 7, 8, 9, A1, A2 |
Due | all digital pins |
101 | all digital pins |
interrupts()
//Staat standaard aan
//Gebruikt na het uitzetten van interrupts
noInterrupts()
//Uitzetten van interrupts
//Kan nodig zijn bij tijd cruciale acties
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
//digitalPinToInterrupt(pin) - mappen van Arduino Pin naar ATmega328p
//ISR - Naam van de functie die wordt aangeroepen als de interrupt optreedt.
//mode - Het soort event waarop de interrupt gaat triggeren
//mode - LOW, CHANGE, RISING, FALLING
detachInterrupt(digitalPinToInterrupt(pin));
//ontkoppelen van de interrupt
volatile int ButtonState = 0;
volatile int state = 0;
void ext_int_callback(){
state != state
};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), ext_int_callback, FAILING);
}
void loop() {
digitalWrite(LED_BUILTIN, state);
}
volatile int state = 0;
void ext_int_callback(){
state != state
};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), ext_int_callback, FAILING);
}
void loop() {
digitalWrite(LED_BUILTIN, state);
}
const int PERIOD_10ms = 0x01
const int PERIOD_14ms = 0x02
const int PERIOD_19ms = 0x03
volatile int state = 0;
void ext_int_callback(){
switch(state)
{
case PERIOD_10ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 14ms;
// Timer Start;
break;
}
case PERIOD_14ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 19ms;
// Timer Start;
break;
}
case PERIOD_19ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 10ms;
// Timer Start;
break;
}
default:
{
/* Timer_ISR entered undefined state */
// Make default period 10ms
break;
}
}
};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), ext_int_callback, FAILING);
}
void loop() {
digitalWrite(LED_BUILTIN, state);
}
Global variables need to be carefully handled while used with ISRs, because interrupts are generally asynchronous and if a global variable is being written into by an ISR, it can get modified at any time. We need to be careful of the following aspects:
Global variables should be modified at only a few necessary places inside a program. If a global variable is being modified by multiple threads (i.e., the main process, ISRs, and other hardware functions like DMA inside an MCU), there is a chance of the variable getting corrupted.
inside a main process is as worrisome as its modification in different threads. Consider the following
Voor zowel de hard -en software vertrekken we van een ...
Bulky oplossing krijgt geen voorkeur
Time Constant | RC Value | `% V_(S)`` |
`0 tau` | `0*RC` | `0 %` |
`0.7 tau` | `0.7*RC` | `50 %` |
`5 tau` | `5*RC` | `100 %` |
Met gebruik veranderen de mechanische eigenschappen van een knop
const int buttonPin = 2;
int buttonState;
int lastButtonState = LOW;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
delay(debounceDelay)
lastButtonState = reading;
}
}
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}