Page 4 of 16
Page 80
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
}
void loop() {
if (digitalRead(4)) {
digitalWrite(2, 1);
} else {
digitalWrite(2, 0);
delay(500);
}
}
Page 82
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
}
void loop() {
while (digitalRead(4) == 1) {};
delay(10);
while (digitalRead(4) == 0) {};
digitalWrite(2, 1);
delay(1000);
digitalWrite(2, 0);
}
Page 83
unsigned long tick_ms(unsigned long offset) {
return millis() + offset;
}
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
}
void loop() {
while (digitalRead(4) == 1) {};
int t = tick_ms(2000);
delay(10);
while (digitalRead(4) == 0) {};
if (tick_ms(0) < t) {
digitalWrite(2, 1);
delay(1000);
digitalWrite(2, 0);
} else {
for (int i = 0; i < 10; i++) {
digitalWrite(2, 1);
delay(1000);
digitalWrite(2, 0);
delay(1000);
}
}
}
Page 84
unsigned long tick_us(unsigned long offset) {
return micros() + offset;
}
void setup() {
pinMode(4, INPUT);
Serial.begin(9600);
}
void loop() {
while (digitalRead(4) == 1) {};
while (digitalRead(4) == 0) {};
int t = tick_us(0);
while (digitalRead(4) == 1) {};
t = tick_us(0) - t;
Serial.println(t);
delay(1000);
}
Page 87
unsigned long tick_us(unsigned long offset) {
return micros() + offset;
}
void delay_until(unsigned long t) {
do {
} while (t > micros());
}
void setup() {
pinMode(4, INPUT_PULLUP);
Serial.begin(9600);
}
unsigned long t, tpush, twait;
int s = 0;
int i;
void loop() {
i = digitalRead(4);
t = tick_us(0);
switch (s) {
case 0: //button not pushed
if (!i) {
s = 1;
tpush = t;
}
break;
case 1: //Button pushed
if (i) {
s = 0;
if ((t - tpush) > 2000000) {
Serial.println("Button held");
} else {
Serial.println("Button pushed");
}
}
break;
default:
s = 0;
}
delay_until(t + 100 * 1000);
}
Page 89
unsigned long tick_us(unsigned long offset) {
return micros() + offset;
}
void delay_until(unsigned long t) {
do {
} while (t > micros());
}
void setup() {
pinMode(4, INPUT_PULLUP);
Serial.begin(9600);
}
unsigned long t, tpush, twait;
int s = 0;
int i;
void loop() {
i = digitalRead(4);
t = tick_us(0);
switch (s) {
case 0: //button not pushed
if (!i) {
s = 1;
tpush = t;
}
break;
case 1: //Button pushed
if (i) {
s = 0;
if ((t - tpush) > 2000000) {
Serial.println("Button held");
} else {
Serial.println("Button pushed");
}
}
break;
default:
s = 0;
}
delay_until(t + 100 * 1000);
}
Page 91
unsigned long tick_us(unsigned long offset) {
return micros() + offset;
}
void delay_until(unsigned long t) {
do {
} while (t > micros());
}
unsigned long t;
int edge;
int buttonNow, buttonState;
int s = 0;
void setup() {
pinMode(4, INPUT_PULLUP);
buttonState = digitalRead(4);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(1, 1);
digitalWrite(2, 0);
digitalWrite(3, 0);
Serial.begin(9600);
}
void loop() {
buttonNow = digitalRead(4);
t = tick_us(0);
edge = buttonNow-buttonState;
buttonState = buttonNow;
switch (s) {
case 0:
if (edge == 1) {
s = 1;
digitalWrite(1, 0);
digitalWrite(2, 1);
digitalWrite(3, 0);
}
break;
case 1:
if (edge == 1) {
s = 2;
digitalWrite(1, 0);
digitalWrite(2, 0);
digitalWrite(3, 1);
}
break;
case 2:
if (edge == 1) {
s = 0;
digitalWrite(1, 1);
digitalWrite(2, 0);
digitalWrite(3, 0);
}
break;
default:
s = 0;
}
delay_until(t + 100 * 1000);
}