Skip to content

Commit efbba34

Browse files
committed
add more examples in section '02. Digital'
1 parent 273171e commit efbba34

22 files changed

Lines changed: 403 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Blink without Delay
2+
3+
Turns on and off a light emitting diode (LED) connected to a digital
4+
pin, without using the delay() function. This means that other code
5+
can run at the same time without being interrupted by the LED code.
6+
7+
The circuit:
8+
* LED attached from pin 3 to VCC.
9+
* Note: on the STM8103 breakout board, there is already an LED on the board
10+
that's attached to pin 3, so no hardware is needed for this example.
11+
12+
created 2005
13+
by David A. Mellis
14+
modified 8 Feb 2010
15+
by Paul Stoffregen
16+
modified 11 Nov 2013
17+
by Scott Fitzgerald
18+
modified 13 Feb 2017 for use with sduino
19+
by Michael Mayer
20+
21+
22+
This example code is in the public domain.
23+
24+
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
25+
*/
26+
27+
#include <Arduino.h>
28+
29+
// constants won't change. Used here to set a pin number :
30+
const int ledPin = 3; // the number of the LED pin
31+
32+
// Variables will change :
33+
int ledState = LOW; // ledState used to set the LED
34+
35+
// Generally, you should use "unsigned long" for variables that hold time
36+
// The value will quickly become too large for an int to store
37+
unsigned long previousMillis = 0; // will store last time LED was updated
38+
39+
// constants won't change :
40+
const long interval = 1000; // interval at which to blink (milliseconds)
41+
42+
void setup() {
43+
// set the digital pin as output:
44+
pinMode(ledPin, OUTPUT);
45+
}
46+
47+
void loop() {
48+
// here is where you'd put code that needs to be running all the time.
49+
50+
// check to see if it's time to blink the LED; that is, if the
51+
// difference between the current time and last time you blinked
52+
// the LED is bigger than the interval at which you want to
53+
// blink the LED.
54+
unsigned long currentMillis = millis();
55+
56+
if (currentMillis - previousMillis >= interval) {
57+
// save the last time you blinked the LED
58+
previousMillis = currentMillis;
59+
60+
// if the LED is off turn it on and vice-versa:
61+
if (ledState == LOW) {
62+
ledState = HIGH;
63+
} else {
64+
ledState = LOW;
65+
}
66+
67+
// set the LED with the ledState of the variable:
68+
digitalWrite(ledPin, ledState);
69+
}
70+
}
71+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Blinking an LED without using the delay() function.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../sduino.mk
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Button
3+
4+
Turns on and off a light emitting diode(LED) connected to digital
5+
pin 3, when pressing a pushbutton attached to pin 2.
6+
7+
8+
The circuit:
9+
* LED attached from pin 3 to VCC
10+
* pushbutton attached to pin 2 from +5V
11+
* 10K resistor attached to pin 2 from ground
12+
13+
* Note: on the STM8S103 breakout board there is already an LED on the board
14+
attached to pin 3.
15+
16+
17+
created 2005
18+
by DojoDave <http://www.0j0.org>
19+
modified 30 Aug 2011
20+
by Tom Igoe
21+
modified 13 Feb 2017 for use with sduino
22+
by Michael Mayer
23+
24+
This example code is in the public domain.
25+
26+
http://www.arduino.cc/en/Tutorial/Button
27+
*/
28+
29+
#include <Arduino.h>
30+
31+
// constants won't change. They're used here to
32+
// set pin numbers:
33+
const int buttonPin = 2; // the number of the pushbutton pin
34+
const int ledPin = 3; // the number of the LED pin
35+
36+
// variables will change:
37+
int buttonState = 0; // variable for reading the pushbutton status
38+
39+
void setup() {
40+
// initialize the LED pin as an output:
41+
pinMode(ledPin, OUTPUT);
42+
// initialize the pushbutton pin as an input:
43+
pinMode(buttonPin, INPUT);
44+
}
45+
46+
void loop() {
47+
// read the state of the pushbutton value:
48+
buttonState = digitalRead(buttonPin);
49+
50+
// check if the pushbutton is pressed.
51+
// if it is, the buttonState is HIGH:
52+
if (buttonState == HIGH) {
53+
// turn LED on:
54+
digitalWrite(ledPin, HIGH);
55+
} else {
56+
// turn LED off:
57+
digitalWrite(ledPin, LOW);
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use a pushbutton to control an LED.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../sduino.mk
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Debounce
3+
4+
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
5+
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
6+
a minimum delay between toggles to debounce the circuit (i.e. to ignore
7+
noise).
8+
9+
The circuit:
10+
* LED attached from pin 3 to VCC
11+
* pushbutton attached from pin 2 to +5V
12+
* 10K resistor attached from pin 2 to ground
13+
14+
* Note: On the STM8S103 breakout board, there is already an LED on the board
15+
connected to pin 3, so you don't need any extra components for this example.
16+
17+
18+
created 21 November 2006
19+
by David A. Mellis
20+
modified 30 Aug 2011
21+
by Limor Fried
22+
modified 28 Dec 2012
23+
by Mike Walters
24+
modified 30 Aug 2016
25+
by Arturo Guadalupi
26+
modified 13 Feb 2017 for use with sduino
27+
by Michael Mayer
28+
29+
30+
This example code is in the public domain.
31+
32+
http://www.arduino.cc/en/Tutorial/Debounce
33+
*/
34+
35+
#include <Arduino.h>
36+
37+
// constants won't change. They're used here to
38+
// set pin numbers:
39+
const int buttonPin = 2; // the number of the pushbutton pin
40+
const int ledPin = 3; // the number of the LED pin
41+
42+
// Variables will change:
43+
int ledState = HIGH; // the current state of the output pin
44+
int buttonState; // the current reading from the input pin
45+
int lastButtonState = LOW; // the previous reading from the input pin
46+
47+
// the following variables are unsigned long's because the time, measured in miliseconds,
48+
// will quickly become a bigger number than can be stored in an int.
49+
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
50+
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
51+
52+
void setup() {
53+
pinMode(buttonPin, INPUT);
54+
pinMode(ledPin, OUTPUT);
55+
56+
// set initial LED state
57+
digitalWrite(ledPin, ledState);
58+
}
59+
60+
void loop() {
61+
// read the state of the switch into a local variable:
62+
int reading = digitalRead(buttonPin);
63+
64+
// check to see if you just pressed the button
65+
// (i.e. the input went from LOW to HIGH), and you've waited
66+
// long enough since the last press to ignore any noise:
67+
68+
// If the switch changed, due to noise or pressing:
69+
if (reading != lastButtonState) {
70+
// reset the debouncing timer
71+
lastDebounceTime = millis();
72+
}
73+
74+
if ((millis() - lastDebounceTime) > debounceDelay) {
75+
// whatever the reading is at, it's been there for longer
76+
// than the debounce delay, so take it as the actual current state:
77+
78+
// if the button state has changed:
79+
if (reading != buttonState) {
80+
buttonState = reading;
81+
82+
// only toggle the LED if the new button state is HIGH
83+
if (buttonState == HIGH) {
84+
ledState = !ledState;
85+
}
86+
}
87+
}
88+
89+
// set the LED:
90+
digitalWrite(ledPin, ledState);
91+
92+
// save the reading. Next time through the loop,
93+
// it'll be the lastButtonState:
94+
lastButtonState = reading;
95+
}
96+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Read a pushbutton, filtering noise.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../sduino.mk
40.2 KB
Loading

0 commit comments

Comments
 (0)