1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-30 16:24:09 +03:00

Committing individual examples instead of one .zip

This commit is contained in:
David A. Mellis
2006-04-26 11:09:43 +00:00
parent 9dbfe53224
commit 260c5d5ee1
39 changed files with 2057 additions and 16 deletions

View File

@ -0,0 +1,29 @@
/* Analog Read to LED
* ------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}

View File

@ -0,0 +1,37 @@
/* Knock Sensor
* ------------
*
* Program using a Piezo element as if it was a knock sensor.
*
* We have to basically listen to an analog pin and detect
* if the signal goes over a certain threshold. It writes
* "knock" to the serial port if the Threshold is crossed,
* and toggles the LED on pin 13.
*
* Created 10 August 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // led connected to control pin 13
int knockSensor = 0; // the knock sensor will be plugged at analog pin 0
byte val = 0; // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status, to toggle the light
int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable "val"
if (val >= THRESHOLD) {
statePin = !statePin; // toggle the status of the ledPin (this trick doesn't use time cycles)
digitalWrite(ledPin, statePin); // turn the led on or off
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline
}
delay(100); // we have to make a delay to avoid overloading the serial port
}

View File

@ -0,0 +1,54 @@
/* Read Jostick
* ------------
*
* Reads two analog pins that are supposed to be
* connected to a jostick made of two potentiometers
*
* We send three bytes back to the computer: one header and
* two with data as signed bytes, this will take the form:
*
* Jxy\r\n
*
* x and y are integers and sent in ASCII
*
* created 20 June 2005
* copyleft 2005 DojoDave for DojoCorp <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // declare pin 13 for the LED
int joyPin1 = 0; // slider variable connecetd to analog pin 0
int joyPin2 = 1; // slider variable connecetd to analog pin 1
int value1 = 0; // variable to read the value from the analog pin 0
int value2 = 0; // variable to read the value from the analog pin 1
void setup()
{
pinMode(ledPin, OUTPUT); // initializes digital pins 0 to 7 as outputs
Serial.begin(9600); // turn on the serial port
}
// function that transformes the data from a scale 0-1024 to a scale 0-9
// and answers back the ASCII value for it
int treatValue(int data)
{
return (data * 9 / 1024) + 48;
}
void loop()
{
value1 = analogRead(joyPin1); // reads the value of the variable resistor
delay(100); // this small pause is needed between reading two
// analog pins, otherwise we get the same value twice
value2 = analogRead(joyPin2); // reads the value of the variable resistor
digitalWrite(ledPin, HIGH); // turn LED on
delay(value1/4); // wait depending on the value read for one axis
digitalWrite(ledPin, LOW); // turn LED off
delay(value2/4); // wait depending on the value read for the other axis
Serial.print('J'); // write a capital 'J' to the serial port
Serial.print(treatValue(value1), BYTE); // send the treated value for sensor 1
Serial.print(treatValue(value2), BYTE); // send the treated value for sensor 2
Serial.println();
}