Skip to content

Commit 961ff73

Browse files
committed
add more examples from Arduino-1.8
1 parent 20e61b1 commit 961ff73

10 files changed

Lines changed: 165 additions & 0 deletions

File tree

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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* Ping))) Sensor
2+
3+
This sketch reads a PING))) ultrasonic rangefinder and returns the
4+
distance to the closest object in range. To do this, it sends a pulse
5+
to the sensor to initiate a reading, then listens for a pulse
6+
to return. The length of the returning pulse is proportional to
7+
the distance of the object from the sensor.
8+
9+
The circuit:
10+
* +V connection of the PING))) attached to +5V
11+
* GND connection of the PING))) attached to ground
12+
* SIG connection of the PING))) attached to digital pin 7
13+
14+
http://www.arduino.cc/en/Tutorial/Ping
15+
16+
created 3 Nov 2008
17+
by David A. Mellis
18+
modified 30 Aug 2011
19+
by Tom Igoe
20+
modified 1 Mar 2017 for use with sduino
21+
by Michael Mayer
22+
23+
This example code is in the public domain.
24+
25+
*/
26+
27+
#include <Arduino.h>
28+
29+
// this constant won't change. It's the pin number
30+
// of the sensor's output:
31+
const int pingPin = 7;
32+
33+
long microsecondsToInches(long microseconds);
34+
long microsecondsToCentimeters(long microseconds);
35+
36+
void setup() {
37+
// initialize serial communication:
38+
Serial_begin(9600);
39+
}
40+
41+
void loop() {
42+
// establish variables for duration of the ping,
43+
// and the distance result in inches and centimeters:
44+
long duration, inches, cm;
45+
46+
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
47+
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
48+
pinMode(pingPin, OUTPUT);
49+
digitalWrite(pingPin, LOW);
50+
delayMicroseconds(2);
51+
digitalWrite(pingPin, HIGH);
52+
delayMicroseconds(5);
53+
digitalWrite(pingPin, LOW);
54+
55+
// The same pin is used to read the signal from the PING))): a HIGH
56+
// pulse whose duration is the time (in microseconds) from the sending
57+
// of the ping to the reception of its echo off of an object.
58+
pinMode(pingPin, INPUT);
59+
duration = pulseInLong(pingPin, HIGH, 200000L);
60+
61+
// convert the time into a distance
62+
inches = microsecondsToInches(duration);
63+
cm = microsecondsToCentimeters(duration);
64+
65+
Serial_print_u(inches);
66+
Serial_print_s("in, ");
67+
Serial_print_u(cm);
68+
Serial_print_s("cm");
69+
Serial_println();
70+
71+
delay(100);
72+
}
73+
74+
long microsecondsToInches(long microseconds) {
75+
// According to Parallax's datasheet for the PING))), there are
76+
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
77+
// second). This gives the distance travelled by the ping, outbound
78+
// and return, so we divide by 2 to get the distance of the obstacle.
79+
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
80+
return microseconds / 74 / 2;
81+
}
82+
83+
long microsecondsToCentimeters(long microseconds) {
84+
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
85+
// The ping travels out and back, so to find the distance of the
86+
// object we take half of the distance travelled.
87+
return microseconds / 29 / 2;
88+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Detecting objects with an ultrasonic range finder.
35.3 KB
Loading
43.3 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
How to make an LED bar graph.
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
LED bar graph
3+
4+
Turns on a series of LEDs based on the value of an analog sensor.
5+
This is a simple way to make a bar graph display. Though this graph
6+
uses 10 LEDs, you can use any number by changing the LED count
7+
and the pins in the array.
8+
9+
This method can be used to control any series of digital outputs that
10+
depends on an analog input.
11+
12+
The circuit:
13+
* 10k Potentiometer, center pin attached to analog input 0
14+
* one side pin (either one) to ground
15+
* the other side pin to +3.3V
16+
* LEDs on pins 0-2, 5 and 7-12 to ground. DO NOT USE the i2c-pins 3 and 4!
17+
18+
The i2c pins lack the capability to drive the output high on their own.
19+
A LED connetected to ground will not work on these pins. You could connect
20+
A LED to Vcc, though. Than it will be lit when a low signal is written to
21+
the pin.
22+
23+
created 4 Sep 2010
24+
by Tom Igoe
25+
modified 1 Mar 2017 for use with sduino
26+
by Michael Mayer
27+
28+
This example code is in the public domain.
29+
30+
http://www.arduino.cc/en/Tutorial/BarGraph
31+
*/
32+
33+
#include <Arduino.h>
34+
35+
// these constants won't change:
36+
const int analogPin = A0; // the pin that the potentiometer is attached to
37+
const int ledCount = 10; // the number of LEDs in the bar graph
38+
39+
int ledPins[] = {
40+
0, 1, 2, 5, 7, 8, 9, 10, 11, 12
41+
}; // an array of pin numbers to which LEDs are attached
42+
43+
44+
void setup() {
45+
// loop over the pin array and set them all to output:
46+
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
47+
pinMode(ledPins[thisLed], OUTPUT);
48+
}
49+
}
50+
51+
void loop() {
52+
// read the potentiometer:
53+
int sensorReading = analogRead(analogPin);
54+
// map the result to a range from 0 to the number of LEDs:
55+
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
56+
57+
// loop over the LED array:
58+
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
59+
// if the array element's index is less than ledLevel,
60+
// turn the pin for this element on:
61+
if (thisLed < ledLevel) {
62+
digitalWrite(ledPins[thisLed], HIGH);
63+
}
64+
// turn off all pins higher than the ledLevel:
65+
else {
66+
digitalWrite(ledPins[thisLed], LOW);
67+
}
68+
}
69+
}
150 KB
Loading
68.6 KB
Loading

0 commit comments

Comments
 (0)