Organizing the arduino things a bit
This commit is contained in:
27
arduino/msaqsg/ch01/HelloWorld/HelloWorld.ino
Normal file
27
arduino/msaqsg/ch01/HelloWorld/HelloWorld.ino
Normal file
@@ -0,0 +1,27 @@
|
||||
const unsigned int LED_PIN = 13;
|
||||
const unsigned int PAUSE = 700;
|
||||
const unsigned int FLASH_DURATION = 100;
|
||||
|
||||
|
||||
void flash(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(FLASH_DURATION);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(FLASH_DURATION);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
for (int i = 1; i < 4; i++) {
|
||||
flash(i);
|
||||
delay(PAUSE);
|
||||
}
|
||||
delay(PAUSE);
|
||||
}
|
1
arduino/msaqsg/ch01/HelloWorld/Makefile
Symbolic link
1
arduino/msaqsg/ch01/HelloWorld/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
54
arduino/msaqsg/ch02/LEDSwitch/LEDSwitch.ino
Normal file
54
arduino/msaqsg/ch02/LEDSwitch/LEDSwitch.ino
Normal file
@@ -0,0 +1,54 @@
|
||||
const unsigned int LED_PIN = 13;
|
||||
const unsigned int BAUD_RATE = 9600;
|
||||
|
||||
void ledOn() {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
Serial.println("LED on");
|
||||
}
|
||||
|
||||
void ledOff() {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
Serial.println("LED off");
|
||||
}
|
||||
|
||||
void ledBlink() {
|
||||
Serial.println("BLINKY TIME");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
ledOn();
|
||||
delay(100);
|
||||
ledOff();
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
void handleInput() {
|
||||
int c = Serial.read();
|
||||
|
||||
switch (c) {
|
||||
case '1':
|
||||
ledOn();
|
||||
break;
|
||||
case '2':
|
||||
ledOff();
|
||||
break;
|
||||
case '3':
|
||||
ledBlink();
|
||||
break;
|
||||
default:
|
||||
Serial.print("Unknown command: ");
|
||||
Serial.println(c);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
Serial.begin(BAUD_RATE);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
handleInput();
|
||||
}
|
1
arduino/msaqsg/ch02/LEDSwitch/Makefile
Symbolic link
1
arduino/msaqsg/ch02/LEDSwitch/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
22
arduino/msaqsg/ch03/BinaryDice/BinaryDice.ino
Normal file
22
arduino/msaqsg/ch03/BinaryDice/BinaryDice.ino
Normal file
@@ -0,0 +1,22 @@
|
||||
const unsigned int LED_BIT0 = 10;
|
||||
const unsigned int LED_BIT1 = 11;
|
||||
const unsigned int LED_BIT2 = 12;
|
||||
|
||||
void outputResult(const long result) {
|
||||
digitalWrite(LED_BIT0, result & B001);
|
||||
digitalWrite(LED_BIT1, result & B010);
|
||||
digitalWrite(LED_BIT2, result & B100);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BIT0, OUTPUT);
|
||||
pinMode(LED_BIT1, OUTPUT);
|
||||
pinMode(LED_BIT2, OUTPUT);
|
||||
|
||||
randomSeed(analogRead(A0));
|
||||
|
||||
outputResult(random(1, 7));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
1
arduino/msaqsg/ch03/BinaryDice/Makefile
Symbolic link
1
arduino/msaqsg/ch03/BinaryDice/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
28
arduino/msaqsg/ch03/DebounceButton/DebounceButton.ino
Normal file
28
arduino/msaqsg/ch03/DebounceButton/DebounceButton.ino
Normal file
@@ -0,0 +1,28 @@
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
const unsigned int LED_PIN = 13;
|
||||
|
||||
int oldButtonState = LOW;
|
||||
int ledState = LOW;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (CURRENT_BUTTON_STATE != oldButtonState &&
|
||||
CURRENT_BUTTON_STATE == HIGH) {
|
||||
if (ledState == LOW) {
|
||||
ledState = HIGH;
|
||||
} else {
|
||||
ledState = LOW;
|
||||
}
|
||||
|
||||
digitalWrite(LED_PIN, ledState);
|
||||
delay(50);
|
||||
}
|
||||
|
||||
oldButtonState = CURRENT_BUTTON_STATE;
|
||||
}
|
1
arduino/msaqsg/ch03/DebounceButton/Makefile
Symbolic link
1
arduino/msaqsg/ch03/DebounceButton/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
32
arduino/msaqsg/ch03/DiceWithButton/DiceWithButton.ino
Normal file
32
arduino/msaqsg/ch03/DiceWithButton/DiceWithButton.ino
Normal file
@@ -0,0 +1,32 @@
|
||||
const unsigned int LED_BIT0 = 10;
|
||||
const unsigned int LED_BIT1 = 11;
|
||||
const unsigned int LED_BIT2 = 12;
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
|
||||
int currentValue = 0;
|
||||
int oldValue = 0;
|
||||
|
||||
void outputResult(const long result) {
|
||||
digitalWrite(LED_BIT0, result & B001);
|
||||
digitalWrite(LED_BIT1, result & B010);
|
||||
digitalWrite(LED_BIT2, result & B100);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BIT0, OUTPUT);
|
||||
pinMode(LED_BIT1, OUTPUT);
|
||||
pinMode(LED_BIT2, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
randomSeed(analogRead(A0));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
currentValue = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (currentValue != oldValue && currentValue == HIGH) {
|
||||
outputResult(random(1, 7));
|
||||
delay(50);
|
||||
}
|
||||
|
||||
oldValue = currentValue;
|
||||
}
|
1
arduino/msaqsg/ch03/DiceWithButton/Makefile
Symbolic link
1
arduino/msaqsg/ch03/DiceWithButton/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
1
arduino/msaqsg/ch03/MoreReliableSwitch/Makefile
Symbolic link
1
arduino/msaqsg/ch03/MoreReliableSwitch/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
@@ -0,0 +1,27 @@
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
const unsigned int LED_PIN = 13;
|
||||
|
||||
int oldButtonState = LOW;
|
||||
int ledState = LOW;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (CURRENT_BUTTON_STATE != oldButtonState &&
|
||||
CURRENT_BUTTON_STATE == HIGH) {
|
||||
if (ledState == LOW) {
|
||||
ledState = HIGH;
|
||||
} else {
|
||||
ledState = LOW;
|
||||
}
|
||||
|
||||
digitalWrite(LED_PIN, ledState);
|
||||
}
|
||||
|
||||
oldButtonState = CURRENT_BUTTON_STATE;
|
||||
}
|
1
arduino/msaqsg/ch03/SimpleButton/Makefile
Symbolic link
1
arduino/msaqsg/ch03/SimpleButton/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
17
arduino/msaqsg/ch03/SimpleButton/SimpleButton.ino
Normal file
17
arduino/msaqsg/ch03/SimpleButton/SimpleButton.ino
Normal file
@@ -0,0 +1,17 @@
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
const unsigned int LED_PIN = 13;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const int BUTTON_STATE = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (BUTTON_STATE == HIGH) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
} else {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
}
|
||||
}
|
1
arduino/msaqsg/ch03/UnreliableSwitch/Makefile
Symbolic link
1
arduino/msaqsg/ch03/UnreliableSwitch/Makefile
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../arduino.mk
|
21
arduino/msaqsg/ch03/UnreliableSwitch/UnreliableSwitch.ino
Normal file
21
arduino/msaqsg/ch03/UnreliableSwitch/UnreliableSwitch.ino
Normal file
@@ -0,0 +1,21 @@
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
const unsigned int LED_PIN = 13;
|
||||
|
||||
int ledState = LOW;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (CURRENT_BUTTON_STATE == HIGH) {
|
||||
ledState = HIGH;
|
||||
} else {
|
||||
ledState = LOW;
|
||||
}
|
||||
|
||||
digitalWrite(LED_PIN, ledState);
|
||||
}
|
Reference in New Issue
Block a user