Updating examples.

This commit is contained in:
David A. Mellis 2007-04-23 15:43:12 +00:00
parent 52a40bfac9
commit a587bc8a4a
44 changed files with 621 additions and 2034 deletions

View File

@ -1,29 +1,27 @@
/* 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
}
/*
* AnalogInput
* by DojoDave <http://www.0j0.org>
*
* 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.
*
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
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,24 @@
// Fading LED
// by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
void setup()
{
// nothing for setup
}
void loop()
{
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
}
}

View File

@ -1,37 +1,34 @@
/* 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
}
/* Knock Sensor
* by DojoDave <http://www.0j0.org>
*
* 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.
*
* http://www.arduino.cc/en/Tutorial/Knock
*/
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,43 @@
/*
* Smoothing
* David A. Mellis <dam@mellis.org>
*
* Reads repeatedly from an analog input, calculating a running average
* and printing it to the computer.
*
* http://www.arduino.cc/en/Tutorial/Smoothing
*/
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.
#define NUMREADINGS 10
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
void setup()
{
Serial.begin(9600); // initialize serial communication with computer
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}
void loop()
{
total -= readings[index]; // subtract the last reading
readings[index] = analogRead(inputPin); // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning
average = total / NUMREADINGS; // calculate the average
Serial.println(average); // send it to the computer (as ASCII digits)
}

View File

@ -0,0 +1,46 @@
// ASCII Table
// by Nicholas Zambetti <http://www.zambetti.com>
void setup()
{
Serial.begin(9600);
// prints title with ending line break
Serial.println("ASCII Table ~ Character Map");
// wait for the long string to be sent
delay(100);
}
int number = 33; // first visible character '!' is #33
void loop()
{
Serial.print(number, BYTE); // prints value unaltered, first will be '!'
Serial.print(", dec: ");
Serial.print(number); // prints value as string in decimal (base 10)
// Serial.print(number, DEC); // this also works
Serial.print(", hex: ");
Serial.print(number, HEX); // prints value as string in hexadecimal (base 16)
Serial.print(", oct: ");
Serial.print(number, OCT); // prints value as string in octal (base 8)
Serial.print(", bin: ");
Serial.println(number, BIN); // prints value as string in binary (base 2)
// also prints ending line break
// if printed last visible character '~' #126 ...
if(number == 126) {
// loop forever
while(true) {
continue;
}
}
number++; // to the next character
delay(100); // allow some time for the Serial data to be sent
}

View File

@ -0,0 +1,74 @@
/*
* Dimmer
* by David A. Mellis
*
* Demonstrates the sending data from the computer to the Arduino board,
* in this case to control the brightness of an LED. The data is sent
* in individual bytes, each of which ranges from 0 to 255. Arduino
* reads these bytes and uses them to set the brightness of the LED.
*
* http://www.arduino.cc/en/Tutorial/Dimmer
*/
int ledPin = 9;
void setup()
{
// begin the serial communication
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
byte val;
// check if data has been sent from the computer
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255)
val = Serial.read();
// set the brightness of the LED
analogWrite(ledPin, val);
}
}
/* Processing code for this example
// Dimmer - sends bytes over a serial port
// by David A. Mellis
import processing.serial.*;
Serial port;
void setup()
{
size(256, 150);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[0], 9600);
// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
//port = new Serial(this, "COM1", 9600);
}
void draw()
{
// draw a gradient from black to white
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}
// write the current X-position of the mouse to the serial port as
// a single byte
port.write(mouseX);
}
*/

View File

@ -0,0 +1,105 @@
/*
* Graph
*
* A simple example of communication from the Arduino board to the computer:
* the value of analog input 0 is printed. We call this "serial"
* communication because the connection appears to both the Arduino and the
* computer as an old-fashioned serial port, even though it may actually use
* a USB cable.
*
* You can use the Arduino serial monitor to view the sent data, or it can
* be read by Processing, Flash, PD, Max/MSP, etc. The Processing code
* below graphs the data received so you can see the value of the analog
* input changing over time.
*
* http://www.arduino.cc/en/Tutorial/Graph
*/
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(analogRead(0));
delay(100);
}
/* Processing code for this example
// Graph
// by David A. Mellis
//
// based on Analog In
// by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
import processing.serial.*;
Serial port;
String buff = "";
int NEWLINE = 10;
// Store the last 64 values received so we can graph them.
int[] values = new int[64];
void setup()
{
size(512, 256);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[0], 9600);
// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
//port = new Serial(this, "COM1", 9600);
}
void draw()
{
background(53);
stroke(255);
// Graph the stored values by drawing a lines between them.
for (int i = 0; i < 63; i++)
line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
while (port.available() > 0)
serialEvent(port.read());
}
void serialEvent(int serial)
{
if (serial != NEWLINE) {
// Store all the characters on the line.
buff += char(serial);
} else {
// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff = buff.substring(0, buff.length()-1);
// Parse the String into an integer. We divide by 4 because
// analog inputs go from 0 to 1023 while colors in Processing
// only go from 0 to 255.
int val = Integer.parseInt(buff)/4;
// Clear the value of "buff"
buff = "";
// Shift over the existing values to make room for the new one.
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];
// Add the received value to the array.
values[63] = val;
}
}
*/

View File

@ -0,0 +1,90 @@
/*
* Physical Pixel
* by David A. Mellis
*
* An example of using the Arduino board to receive data from the
* computer. In this case, the Arduino boards turns on an LED when
* it receives the character 'H', and turns off the LED when it
* receives the character 'L'.
*
* The data can be sent from the Arduino serial monitor, or another
* program like Processing (see code below), Flash (via a serial-net
* proxy), PD, or Max/MSP.
*
* http://www.arduino.cc/en/Tutorial/PhysicalPixel
*/
int outputPin = 13;
int val;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
}
void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'H') {
digitalWrite(outputPin, HIGH);
}
if (val == 'L') {
digitalWrite(outputPin, LOW);
}
}
}
/* Processing code for this example
// mouseover serial
// by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
// Demonstrates how to send data to the Arduino I/O board, in order to
// turn ON a light if the mouse is over a rectangle and turn it off
// if the mouse is not.
// created 13 May 2004
import processing.serial.*;
Serial port;
void setup()
{
size(200, 200);
noStroke();
frameRate(10);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}
// function to test if mouse is over square
boolean mouseOverRect()
{
return ((mouseX >= 50)&&(mouseX <= 150)&&(mouseY >= 50)&(mouseY <= 150));
}
void draw()
{
background(#222222);
if(mouseOverRect()) // if mouse is over square
{
fill(#BBBBB0); // change color
port.write('H'); // send an 'H' to indicate mouse is over square
} else {
fill(#666660); // change color
port.write('L'); // send an 'L' otherwise
}
rect(50, 50, 100, 100); // draw square
}
*/

View File

@ -0,0 +1,89 @@
int redPin = 0;
int greenPin = 1;
int bluePin = 2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("R");
Serial.println(analogRead(redPin));
Serial.print("G");
Serial.println(analogRead(greenPin));
Serial.print("B");
Serial.println(analogRead(bluePin));
delay(100);
}
/* Processing code for this example
// Color Mixer
// by David A. Mellis
//
// Created 2 December 2006
//
// based on Analog In
// by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
//
// Created 8 February 2003
// Updated 2 April 2005
import processing.serial.*;
String buff = "";
int rval = 0, gval = 0, bval = 0;
int NEWLINE = 10;
Serial port;
void setup()
{
size(200, 200);
// Print a list in case COM1 doesn't work out
println("Available serial ports:");
println(Serial.list());
//port = new Serial(this, "COM1", 9600);
// Uses the first available port
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
while (port.available() > 0) {
serialEvent(port.read());
}
background(rval, gval, bval);
}
void serialEvent(int serial)
{
// If the variable "serial" is not equal to the value for
// a new line, add the value to the variable "buff". If the
// value "serial" is equal to the value for a new line,
// save the value of the buffer into the variable "val".
if(serial != NEWLINE) {
buff += char(serial);
} else {
// The first character tells us which color this value is for
char c = buff.charAt(0);
// Remove it from the string
buff = buff.substring(1);
// Discard the carriage return at the end of the buffer
buff = buff.substring(0, buff.length()-1);
// Parse the String into an integer
if (c == 'R')
rval = Integer.parseInt(buff);
else if (c == 'G')
gval = Integer.parseInt(buff);
else if (c == 'B')
bval = Integer.parseInt(buff);
// Clear the value of "buff"
buff = "";
}
}
*/

View File

@ -0,0 +1,25 @@
/*
* Blink
*
* The basic Arduino example. Turns on an LED on for one second,
* then off for one second, and so on... We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* http://www.arduino.cc/en/Tutorial/Blink
*/
int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

View File

@ -0,0 +1,27 @@
/*
* Button
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7.
*
* http://www.arduino.cc/en/Tutorial/Button
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}

View File

@ -0,0 +1,37 @@
/*
* Loop
* by David A. Mellis
*
* Lights multiple LEDs in sequence, then in reverse. Demonstrates
* the use of a for() loop and arrays.
*
* http://www.arduino.cc/en/Tutorial/Loop
*/
int timer = 100; // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)
void setup()
{
int i;
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output
}
void loop()
{
int i;
for (i = 0; i < num_pins; i++) { // loop through each pin...
digitalWrite(pins[i], HIGH); // turning it on,
delay(timer); // pausing,
digitalWrite(pins[i], LOW); // and turning it off.
}
for (i = num_pins - 1; i >= 0; i--) {
digitalWrite(i, HIGH);
delay(timer);
digitalWrite(i, LOW);
}
}

View File

@ -1,31 +0,0 @@
/* Basic Digital Read
* ------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
* concept of Active-Low, which consists in connecting buttons using a
* 1K to 10K pull-up resistor.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}

View File

@ -1,24 +0,0 @@
/* Blink when Digital Read
* ------------------------
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // blink the LED and go OFF
delay(200);
digitalWrite(ledPin, LOW);
delay(1000);
}
}

View File

@ -1,110 +0,0 @@
/* LCD 8bits
* ---------
*
* This is the first example in how to use an LCD screen
* configured with data transfers over 8 bits. The example
* uses all the digital pins on the Arduino board, but can
* easily display data on the display
*
* There are the following pins to be considered:
*
* - DI, RW, DB0..DB7, Enable (11 in total)
*
* the pinout for LCD displays is standard and there is plenty
* of documentation to be found on the internet.
*
* Picture at:
* http://arduino.berlios.de/index.php/Tutorial/LCD8Bits
*
* (cleft) 2005 DojoDave for K3
*
*/
int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};
int Enable = 2;
void LcdCommandWrite(int value) {
// poll all the pins
int i = 0;
for (i=DB[0]; i <= DI; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(Enable,HIGH);
delayMicroseconds(1); // pause 1 ms according to datasheet
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}
void LcdDataWrite(int value) {
// poll all the pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}
void setup (void) {
int i = 0;
for (i=Enable; i <= DI; i++) {
pinMode(i,OUTPUT);
}
delay(100);
// initiatize lcd after a short pause
// needed by the LCDs controller
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(64);
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(50);
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(20);
LcdCommandWrite(0x06); // entry mode set:
// increment automatically, no display shift
delay(20);
LcdCommandWrite(0x0E); // display control:
// turn display on, cursor on, no blinking
delay(20);
LcdCommandWrite(0x01); // clear display, set cursor position to zero
delay(100);
LcdCommandWrite(0x80); // display control:
// turn display on, cursor on, no blinking
delay(20);
}
void loop (void) {
LcdCommandWrite(0x02); // set cursor position to zero
delay(10);
// Write the welcome message
LcdDataWrite('H');
LcdDataWrite('o');
LcdDataWrite('l');
LcdDataWrite('a');
LcdDataWrite(' ');
LcdDataWrite('C');
LcdDataWrite('a');
LcdDataWrite('r');
LcdDataWrite('a');
LcdDataWrite('c');
LcdDataWrite('o');
LcdDataWrite('l');
LcdDataWrite('a');
delay(500);
}

View File

@ -1,28 +0,0 @@
/* Blinking LED
* ------------
*
* turns on and off a light emitting diode(LED) connected to a digital
* pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino
* board because it has a resistor attached to it, needing only an LED
*
* Created 1 June 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
* based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

View File

@ -1,86 +0,0 @@
/* Knight Rider 1
* --------------
*
* Knight Rider is an AI car that showed up in the TV series from
* the 80's with David Hasselhoff. The car had plenty of LED effects.
*
* Basically this is an extension of Blink_LED. This program handles
* 6 LEDs connected to pins 2 to 7. This is the first of a series of
* three examples useful to understand the for(;;) loop
*
* Picture at:
* http://arduino.berlios.de/index.php/Tutorial/KnightRider
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int timer = 100;
void setup(){
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
}
void loop() {
digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);
digitalWrite(pin7, HIGH);
delay(timer);
digitalWrite(pin7, LOW);
delay(timer);
digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
}

View File

@ -1,46 +0,0 @@
/* Knight Rider 2
* --------------
*
* Knight Rider is an AI car that showed up in the TV series from
* the 80's with David Hasselhoff. The car had plenty of LED effects.
*
* Basically this is an extension of Blink_LED. This program handles
* 6 LEDs connected to pins 2 to 7. This is the second of a series of
* three examples useful to understand the for(;;) loop
*
* In this example we declare an array with the pin numbers we use as inputs,
* then we browse the array up and down to turn the different pins on/off
*
* Picture at:
* http://arduino.berlios.de/index.php/Tutorial/KnightRider
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pinArray[] = {2, 3, 4, 5, 6, 7};
int count = 0;
int timer = 100;
void setup(){
// we make all the declarations at once
for (count=0;count<6;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
for (count=0;count<6;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
for (count=5;count>=0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
}

View File

@ -1,51 +0,0 @@
/* Knight Rider 3
* --------------
*
* Knight Rider is an AI car that showed up in the TV series from
* the 80's with David Hasselhoff. The car had plenty of LED effects.
*
* Basically this is an extension of Blink_LED. This program handles
* 6 LEDs connected to pins 2 to 7. This is the third of a series of
* three examples useful to understand the for(;;) loop
*
* In this example we declare an array with the pin numbers we use as inputs,
* then we browse the array up and down to turn the different pins on/off
*
* This example concentrates in making the visuals fluid.
*
* Picture at:
* http://arduino.berlios.de/index.php/Tutorial/KnightRider
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pinArray[] = {2, 3, 4, 5, 6, 7};
int count = 0;
int timer = 30;
void setup(){
for (count=0;count<6;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=5;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
}

View File

@ -1,195 +0,0 @@
int serialVal = 0;
int value = 0;
int i = 0;
boolean reading = false;
boolean writing = false;
int times = 0;
int serialBuff[7];
// define wiring pins
byte pin_max7219_clock = 4;
byte pin_max7219_load = 2;
byte pin_max7219_dataIn = 3;
// specify wiring pin i/o directions
void setPinModes()
{
pinMode(pin_max7219_dataIn, OUTPUT);
pinMode(pin_max7219_clock, OUTPUT);
pinMode(pin_max7219_load, OUTPUT);
}
// define max7219 registers
byte max7219_reg_noop = 0x00;
byte max7219_reg_digit0 = 0x01;
byte max7219_reg_digit1 = 0x02;
byte max7219_reg_digit2 = 0x03;
byte max7219_reg_digit3 = 0x04;
byte max7219_reg_digit4 = 0x05;
byte max7219_reg_digit5 = 0x06;
byte max7219_reg_digit6 = 0x07;
byte max7219_reg_digit7 = 0x08;
byte max7219_reg_decodeMode = 0x09;
byte max7219_reg_intensity = 0x0a;
byte max7219_reg_scanLimit = 0x0b;
byte max7219_reg_shutdown = 0x0c;
byte max7219_reg_displayTest = 0x0f;
// define max7219 as rows and cols (for nexus 8x8 displays)
byte max7219_row0 = 0x01;
byte max7219_row1 = 0x02;
byte max7219_row2 = 0x03;
byte max7219_row3 = 0x04;
byte max7219_row4 = 0x05;
byte max7219_row5 = 0x06;
byte max7219_row6 = 0x07;
byte max7219_row7 = 0x08;
byte max7219_col0 = 0x80;
byte max7219_col1 = 0x01;
byte max7219_col2 = 0x02;
byte max7219_col3 = 0x04;
byte max7219_col4 = 0x08;
byte max7219_col5 = 0x10;
byte max7219_col6 = 0x20;
byte max7219_col7 = 0x40;
// function to control max7219 data line
void max7219_setData(boolean value)
{
digitalWrite(pin_max7219_dataIn, value);
}
// function to control max7219 clock line
void max7219_setClock(boolean value)
{
digitalWrite(pin_max7219_clock, value);
}
// function to control max7219 load line
void max7219_setLoad(boolean value)
{
digitalWrite(pin_max7219_load, value);
}
// function that puts a byte of data to the max7219
void max7219_putByte(byte data)
{
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
max7219_setClock(LOW); // tick
if (data & mask){ // choose bit
max7219_setData(HIGH); // send 1
}else{
max7219_setData(LOW); // send 0
}
max7219_setClock(HIGH); // tock
--i; // move to lesser bit
}
}
// function that puts a byte of data into a max7219 register
void max7219_put(byte reg, byte data)
{
max7219_setLoad(HIGH); // begin
max7219_putByte(reg); // specify register
max7219_putByte(data); // put data
max7219_setLoad(LOW); // latch in data
max7219_setLoad(HIGH); // end
}
// function that sets brightness of the max7219
void max7219_setIntensity(byte intensity)
{
// range: 0x00 to 0x0f
max7219_put(max7219_reg_intensity, intensity & 0x0f);
}
////////////////////////////////////////////
// function that sets the same value for all registers of the max7219
void max7219_all(byte value)
{
max7219_put(0x01, value);
max7219_put(0x02, value);
max7219_put(0x03, value);
max7219_put(0x04, value);
max7219_put(0x05, value);
max7219_put(0x06, value);
max7219_put(0x07, value);
max7219_put(0x08, value);
}
///////////////////////////////////////////
void printBuff(){
for (i=1;i <= 8;i++){
max7219_put(0x0+i, serialBuff[i-1]);
}
}
////////////////////////////////////////////
void readBuff(){
Serial.print("into readbuff method");
serialVal = Serial.read();
if (serialVal != -1){
Serial.print("information there");
if ((serialVal == 43) && !reading){
reading = true;
Serial.print("plus");
}
if ((serialVal == 45) && (times == 0 && reading)){
writing = true;
Serial.print("minus");
}
if (reading && writing){
serialBuff[times] = serialVal;
times++;
}
if (times >= 7){
Serial.print("Print to buff");
times = 0;
reading = false;
writing = false;
printBuff();
}
}
}
//////////////////////////////////////////////////////////////
// function that initializes the max7219 to use a matrix of leds
void max7219_init()
{
max7219_put(max7219_reg_scanLimit, 0x07); // use all 8 columns
max7219_put(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
max7219_put(max7219_reg_shutdown, 0x01); // not in shutdown mode
max7219_put(max7219_reg_displayTest, 0x00); // no display test
max7219_all(0x00); // empty registers
max7219_setIntensity(0x0f); // set initial brightness to dim
}
// program initialization routine
void setup()
{
Serial.begin(9600);
setPinModes();
max7219_init();
}
// program loop
void loop()
{
Serial.print("in the loop");
//max7219_all(0x00);
//delay(500);
readBuff();
}

View File

@ -1,79 +0,0 @@
/* Shift Out
* ---------
*
* This example uses the LED driver 4794 from Philips to drive
* 8 LEDs at once. The 4794 is a chip that can be chained so that
* we will be able of adding 8 outputs each time you add a new chip
*
* We use four pins to connect to the chip:
*
* - data: this one sends the data out to the chip
*
* - strob: controls the load of data to the chip's output
*
* - clock: synchronizes the load of data
*
* - oe: turns the output on/off, we use it to control the luminosity of the LEDs
*
* Pictures at:
* http://arduino.berlios.de/index.php/Tutorial/LEDDriver
*
* (copyleft) 2005 K3, Malmo University
* @author: David Cuartielles, Marcus Hannerstig
* @hardware: David Cuartielles, Marcos Yarza
* @project: SMEE - Experiential Vehicles
*
*/
int data = 9;
int strob = 8;
int clock = 10;
int oe = 11;
int count = 0;
int dato = 0; // dato is a varible we use to send data to the LEDs
void setup()
{
//Serial.begin(9600); // uncomment the serial-related lines to monitor the program's progress
pinMode(data, OUTPUT); // declare all the control pins as outputs
pinMode(clock, OUTPUT);
pinMode(strob, OUTPUT);
pinMode(oe, OUTPUT);
}
// sends a pulse to the 4794 indicating that
// it is time to load data
void PulseClock(void) {
digitalWrite(clock, LOW);
delayMicroseconds(20);
digitalWrite(clock, HIGH);
delayMicroseconds(50);
digitalWrite(clock, LOW);
}
void loop()
{
dato = 129; // if dato equals 129 the first and last LED will be on
// go through the "dato" variable and send it bit by bit over the data pin
for (count = 0; count < 8; count++) {
digitalWrite(data, dato & 01);
//Serial.print((dato & 01) + 48, BYTE);
dato>>=1;
if (count == 7){
digitalWrite(oe, LOW);
digitalWrite(strob, HIGH);
}
PulseClock();
digitalWrite(oe, HIGH);
}
delayMicroseconds(20);
digitalWrite(strob, LOW);
delay(100);
//Serial.println();
delay(100); // waits for a second
}

View File

@ -1,35 +0,0 @@
/* DC motor
* --------
*
* Switch a motor on and off making use of a transistor
* we use of a BD137 from Fairchild. It is possible to
* play with the motor's innertia, and use a potentiometer to
* control the speed.
*
* (cleft) 2005 DojoDave for DojoCorp at Madrid Medialab - Spain
*/
int motorPin = 6; // selec the pin where the motor is connected at
int value = 0; // variable to store the reading from the potentiometer
int potPin = 0; // analog pin where to plug the potentiometer at
void setup() {
pinMode(motorPin, OUTPUT); // declare the motor as an output
Serial.begin(9600); // connect to the serial port to send values back
}
void loop() {
// read the potentiometer
value = analogRead(potPin);
// print its value back to the computer
Serial.println(value);
// turn the motor on
digitalWrite(motorPin,HIGH);
delay(50);
digitalWrite(motorPin,LOW);
// this will control how long the motor is off
delay(value);
}

View File

@ -1,127 +0,0 @@
/* Driving two DC motors
* -----------------------
*
* Custom timer originally intended to DC motor
* control purposes and made from two nested loops:
* - "scan cycle" is the main loop
* - "control cycle" is the internal loop
* Timing id adjusted by changing number of iterations en each loop
* and the internal delay of each control cycle iteration (tic). If scan
* cycle code takes significative time to jam current control cycle, this
* delay could be easily reduced or bypassed.
*
* (copyleft) 2005 by Quique
* <mailto:info@spindesk.com>
* posted to the Arduino Forum
* <http://www.arduino.cc>
*
*/
int ledPin0 = 13; // LED connected to digital pin 13
boolean ledValue = LOW;
int motorPin1 = 9; // Motor 1 connected to digital pin 9
int motorPin2 = 8; // Motor 1 connected to digital pin 8
int potPin1 = 5; // Potentiometer1 connected to analog pin 5 ( 5 is 1 in some boards)
int potPin2 = 4; // Potentiometer1 connected to analog pin 5 ( 4 is 2 in some boards)
// Timing Setup
int scanCycle = 10; // Number of control cycles in a scan cycle
int controlCycle = 200; // Control cycle iterations
int tic = 6; // Control cycle iteration aditional delay in microseconds
int currentSCycle = 0; // Current scan cycle iteration
int currentCCycle = 0; // Current control cycle iteration
boolean scanEnable = true; // Allows read analog & digital inputs
// End timing setup
int counter = 0; // Scan cycle counter used to change led status
int motor1_PW; // motor 1 Pulse Width
int motor2_PW; // motor 2 Pulse Width
/*
* Switch the boolean value assigned to any variable
*/
boolean boolSwitch (boolean *target)
{
if ( *target ) {
*target = false;
}
else {
*target = true;
}
return *target;
}
void setup()
{
pinMode (ledPin0, OUTPUT); // sets the digital pin as output
pinMode (motorPin1, OUTPUT); // sets the digital pin as output
pinMode (motorPin2, OUTPUT); // sets the digital pin as output
}
void loop()
{
// Scan cycle
if (scanEnable)
{
//
// Scan cycle logic here
//
motor1_PW = analogRead (potPin1)/5; // Pot 1 read scale change
motor2_PW = analogRead (potPin2)/5; // Pot 1 read scale change
// Swith led in pin 13 each 10 scan cycles. We can assume that while
// Led is on (or off), porgram has taken 10 scan cycles. So, if we adjust
// blink time to 1 sec, we are able to scanning inputs each 100 msec.
if (counter++ >= 10)
{
digitalWrite (ledPin0, boolSwitch (&ledValue)); // Led blink each 10 scan cycles i.e. if
// led is on 1 second, we are scaning knobs each
// 100 miliseconds
counter =0;
}
}
// Control cycle
for (currentCCycle = 0; currentCCycle < controlCycle; currentCCycle ++)
{
delayMicroseconds (tic);
//
// Control cycle logic here
//
if ( motor1_PW > currentCCycle )
{
digitalWrite ( motorPin1, LOW);
}
else
{
digitalWrite ( motorPin1, HIGH);
}
if ( motor2_PW > currentCCycle )
{
digitalWrite ( motorPin2, LOW);
}
else
{
digitalWrite ( motorPin2, HIGH);
}
}
// Detect completed scan cycle
if ( currentSCycle ++ > scanCycle)
{
scanEnable = true; // Allow readings of external inputs
currentSCycle = 0; // Reset scan cycle counter
}
else
{
scanEnable = false; //
}
}

View File

@ -1,57 +0,0 @@
/* Stepper Copal Unipolar
* ----------------------
*
* Program to drive a stepper motor coming from a 5'25 disk drive
* according to the documentation I found, this stepper: "[...] motor
* made by Copal Electronics, with 1.8 degrees per step and 96 ohms
* per winding, with center taps brought out to separate leads [...]"
* [http://www.cs.uiowa.edu/~jones/step/example.html]
*
* It is a bipolar stepper motor with 5 wires:
*
* - red: power connector, I have it at 5V and works fine
* - orange and black: coil 1
* - brown and yellow: coil 2
*
* (cleft) 2005 DojoDave for K3
* http://www.0j0.org | http://arduino.berlios.de
*
* @author: David Cuartielles
* @date: 20 Oct. 2005
*/
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 500;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop() {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
}

View File

@ -1,73 +0,0 @@
/* Stepper Unipolar Advanced
* -------------------------
*
* Program to drive a stepper motor coming from a 5'25 disk drive
* according to the documentation I found, this stepper: "[...] motor
* made by Copal Electronics, with 1.8 degrees per step and 96 ohms
* per winding, with center taps brought out to separate leads [...]"
* [http://www.cs.uiowa.edu/~jones/step/example.html]
*
* It is a bipolar stepper motor with 5 wires:
*
* - red: power connector, I have it at 5V and works fine
* - brown and black: coil 1
* - orange and yellow: coil 2
*
* We use a potentiometer to control the speed and direction of the motor
*
* (cleft) 2005 DojoDave for K3
* http://www.0j0.org | http://arduino.berlios.de
*
* @author: David Cuartielles
* @date: 20 Oct. 2005
*/
int ledPin = 13;
int statusLed = LOW;
int motorPins[] = {8, 9, 10, 11};
int count = 0;
int count2 = 0;
int delayTime = 500;
int val = 0;
void setup() {
pinMode(ledPin, OUTPUT);
for (count = 0; count < 4; count++) {
pinMode(motorPins[count], OUTPUT);
}
}
void moveForward() {
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count--) {
digitalWrite(motorPins[count], count2>>count&0x01);
}
delay(delayTime);
}
void moveBackward() {
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count--) {
digitalWrite(motorPins[3 - count], count2>>count&0x01);
}
delay(delayTime);
}
void loop() {
val = analogRead(0);
if (val > 540) {
delayTime = 2048 - 1024 * val / 512 + 1; // move faster the higher the value from the potentiometer
moveForward();
} else if (val < 480) {
delayTime = 1024 * val / 512 + 1; // move faster the lower the value from the potentiometer
moveBackward();
} else {
delayTime = 1024;
}
}

View File

@ -1,42 +0,0 @@
/* Potentiometer to Processing
* ---------------------------
*
* This program sends data from a potentiometer to Processing
* over the serial port (works in PC: Windows and Linux, and MAC)
* The code reads a potentiometer plugged to an analog
* input and sends the data as a byte back to the computer.
*
* In order to make the data transfer as simple as possible
* we will only send a byte back to the computer, what means
* that the data coming from the ADC will be divided by 4.
*
* (cleft) 2005 DojoDave for K3
*
* @author: David Cuartielles
* @context: ID3 - K3 - MAH - Sweden
*/
int ledPin = 13; // pin for the LED
int potPin = 0; // analog pin for the potentiometer
int ledStatus = LOW; // we use a variable to toggle the LEDs state
int val = 0; // variable to store the potentiometer's reading
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an output
Serial.begin(9600); // initialize the serial port
}
void loop() {
// read the potentiometer's value and store it
val = analogRead(potPin)/4;
// send the value over the port
Serial.print(val, BYTE);
// wait a bit
delay(100);
// change the state of the LED (if HIGH, then LOW, and viceversa)
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
}

View File

@ -1,29 +0,0 @@
/* PROGRAM'S TITLE
* ---------------
*
* Comments
*
* (copyright notice) 2005 by NAME
* <http://youraddress.here>
* <mailto:you@youraddress.here>
*
*/
// variable declaration
// type name_of_var = value;
// function declaration
// type name_of_function(type parameters) {
// code_block;
// }
void setup(void) {
// initialize inputs/outputs
// start serial port
}
void loop(void) {
// program here
// code_block;
}

View File

@ -1,71 +0,0 @@
/* Inputs to PD
* -------------------
*
* This program sends data from a bunch of inputs to PD
* over the serial port (works on PC: Windows and Linux, and MAC)
* The code reads 3 potentiometers and 6 buttons plugged to Arduino
* input and sends the data back to the computer.
*
* On the other side there will be a PureData sketch running
* comport2000 and will use the data to change a sound or video
* file properties.
*
* The buttons will be characterized with '1' or '0' depending on
* their state, while potentiometers will be characterized with a
* 10 bits integer in the range 0..1024
*
* The first sensor will be marked with 'A' (ascii 65), the second
* with 'B', and so on. The end of sensor reading is marked with
* the characters EOLN (ascii 10).
*
* (cleft) 2005 DojoDave for K3
*
* @author: David Cuartielles
* @context: ID3 - K3 - MAH - Sweden
*/
int ledPin = 13; // declare the pin with the LED
int potentioMeter = 0; // declare the analog pin for the potentiometer
int pushButton = 0; // declare the value
int writeChar = 65; // declare the first reading as 'A'
int value = 0; // value to read the different sensors
int ledStatus = LOW; // status of the LED
void setup() {
pinMode(ledPin, OUTPUT); // declare the LED as output
Serial.begin(9600); // intitialize the serial port
}
void loop() {
writeChar = 65; //Sets the sensor idendifier
// character back to 'A'.
// start reading the potentiometers on the analog pins
for(potentioMeter=0;potentioMeter<3;potentioMeter++){
Serial.print(writeChar, BYTE);
Serial.print(analogRead(potentioMeter));
Serial.println();
writeChar = writeChar + 1;
delay(10);
}
// read the pushbuttons
for(pushButton=2;pushButton<8;pushButton++){
Serial.print(writeChar, BYTE);
value = digitalRead(pushButton); // reads the value at a digital input
if (value)
{
Serial.print('0');
} else {
Serial.print('1');
}
Serial.println();
writeChar = writeChar + 1;
delay(10);
}
delay(100);
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
}

View File

@ -1,37 +0,0 @@
/* Potentiometer to PD
* -------------------
*
* This program sends data from a potentiometer to PD
* over the serial port (works in Windows and Linux, MAC?)
* The code reads a potentiometer plugged to an analog
* input and sends the data as a byte back to the computer.
*
* On the other side there will be a PureData sketch running
* comport2000 and will use the data to change a sound file
* properties.
*
* In order to make the data transfer as simple as possible
* we will only send a byte back to the computer, what means
* that the data coming from the ADC will be divided by 4.
*
* (cleft) 2005 DojoDave for K3
*
* @author: David Cuartielles
* @context: ID3 - K3 - MAH - Sweden
*/
int ledPin = 13;
int potPin = 0;
int ledStatus = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.print(analogRead(potPin)/4, BYTE);
delay(100);
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
}

View File

@ -1,31 +0,0 @@
/* Two Potentiometers
* ------------------
*
* This program reads two potentiometers and
* sends the data to the computer. It combines
* both with PD or Processing
*
* (cleft) 2005 David Cuartielles for DojoCorp
* @author: D. Cuartielles
* @credit: Nima and Greg
* @date: 2005-11-18
* @location: SFU, Vancouver, Canada
*/
int potPin1 = 0;
int potPin2 = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print('A');
Serial.print(analogRead(potPin1));
Serial.println();
delay(500);
Serial.print('B');
Serial.print(analogRead(potPin2));
Serial.println();
delay(500);
}

View File

@ -1,60 +0,0 @@
/* Keyboard Serial
* ----------------
*
* Program to play tones depending on the
* data coming from the serial port.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
byte names[] ={'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte val = 0;
int serByte = -1;
int statePin = LOW;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(speakerOut, LOW);
serByte = Serial.read();
if (serByte != -1) {
val = serByte;
Serial.print(val);
statePin = !statePin;
digitalWrite(ledPin, statePin);
}
for (count=0;count<=8;count++) {
if (names[count] == val) {
digitalWrite(speakerOut, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count]);
}
}
}

View File

@ -1,62 +0,0 @@
/* Keyboard Serial
* ----------------
*
* Program to play tones depending on the
* data coming from the serial port.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* We use the PWM on pin 9 and analogWrite to change volume
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
int volume = 300; // maximum volume is 1000
byte names[] ={'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte val = 0;
int serByte = -1;
int statePin = LOW;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
analogWrite(speakerOut, 0);
serByte = Serial.read();
if (serByte != -1) {
val = serByte;
Serial.print(val);
statePin = !statePin;
digitalWrite(ledPin, statePin);
}
for (count=0;count<=8;count++) {
if (names[count] == val) {
analogWrite(speakerOut,volume);
delayMicroseconds(tones[count]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count]);
}
}
}

View File

@ -1,67 +0,0 @@
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

View File

@ -1,67 +0,0 @@
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

View File

@ -1,69 +0,0 @@
/* Play Melody - FD
* ----------------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* We use the PWM on pin 9 with analogWrite to change volume
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
int volume = 300; // maximum volume is 1000
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,volume);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

View File

@ -1,91 +0,0 @@
/* Accelerometer Sensor
* --------------------
*
* Reads an 2-D accelerometer
* attached to a couple of digital inputs and
* sends their values over the serial port; makes
* the monitor LED blink once sent
*
*
* http://www.0j0.org
* copyleft 2005 K3 - Malmo University - Sweden
* @author: Marcos Yarza
* @hardware: Marcos Yarza
* @project: SMEE - Experiential Vehicles
* @sponsor: Experiments in Art and Technology Sweden, 1:1 Scale
*/
int ledPin = 13;
int xaccPin = 7;
int yaccPin = 6;
int value = 0;
int accel = 0;
char sign = ' ';
int timer = 0;
int count = 0;
void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT);
pinMode(xaccPin, INPUT);
pinMode(yaccPin, INPUT);
}
/* (int) Operate Acceleration
* function to calculate acceleration
* returns an integer
*/
int operateAcceleration(int time1) {
return abs(8 * (time1 / 10 - 500));
}
/* (void) readAccelerometer
* procedure to read the sensor, calculate
* acceleration and represent the value
*/
void readAcceleration(int axe){
timer = 0;
count = 0;
value = digitalRead(axe);
while(value == HIGH) { // Loop until pin reads a low
value = digitalRead(axe);
}
while(value == LOW) { // Loop until pin reads a high
value = digitalRead(axe);
}
while(value == HIGH) { // Loop until pin reads a low and count
value = digitalRead(axe);
count = count + 1;
}
timer = count * 18; //calculate the teme in miliseconds
//operate sign
if (timer > 5000){
sign = '+';
}
if (timer < 5000){
sign = '-';
}
//determine the value
accel = operateAcceleration(timer);
//Represent acceleration over serial port
if (axe == xaccPin){
Serial.print('X');
} else {
Serial.print('Y');
}
Serial.print(sign);
Serial.print(accel);
Serial.println();
}
void loop() {
readAcceleration(xaccPin); //reads and represents acceleration X
readAcceleration(yaccPin); //reads and represents acceleration Y
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
}

View File

@ -1,73 +0,0 @@
/* Ultrasound Sensor
* -----------------
*
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport. The sensor is the
* so-called PING sensor from Parallax/Devantech.
*
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*
*/
int ultraSoundSignal = 8; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}
void loop() {
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse
* -------------------------------------------------------------------
*/
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}
/* Writing out values to the serial port
* -------------------------------------------------------------------
*/
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
Serial.print('A'); // Example identifier for the sensor
Serial.print(ultrasoundValue);
Serial.println();
/* Lite up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
/* Delay of program
* -------------------------------------------------------------------
*/
delay(100);
}

View File

@ -1,54 +0,0 @@
/* 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();
}

View File

@ -1,32 +0,0 @@
/* Analog Read Send
* ----------------
*
* 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. Sends the data back to a computer
* over the serial port.
*
* 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
Serial.begin(9600); // use the serial port to send the values back to the computer
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
Serial.println(val); // print the value to the serial port
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

@ -1,37 +0,0 @@
/* Double Counter
* --------------
*
* This program creates a double counter
* and sends the information over the port
* back to the computer.
* It can be used to test the connection of two different
* sensors to the board at the same time.
*
* (cleft) 2005 David Cuartielles for DojoCorp
* @author: D. Cuartielles
* @credits: Greg and Nima from SFU
*/
int count = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
// counter A will go forwards
Serial.print('A');
Serial.print(count);
Serial.println();
delay(1000);
// counter B will go backwards
Serial.print('B');
Serial.print(1024 - count);
Serial.println();
delay(1000);
// increase and reset the counter (if needed)
count++;
if (count == 1024) count = 0;
}

View File

@ -1,86 +0,0 @@
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 04_function development
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* the same as examlpe 03 but it deploys 2 reusable functions.
* for doing the same job.
* readSerialString() and printSerialString()
* you just need to instantiate an array that will hold all the chars of the string
* I've put a 100 value for excess, but if you exactly know how many bytes you are expecting
* simply write it down inside the brackets [yourLengthHere]
*
* created 16 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(Serial.available()) {
Serial.print("reading Serial String: "); //optional: for confirmation
while (serialAvailable()){
strArray[i] = Serial.read();
i++;
Serial.print(strArray[(i-1)]); //optional: for confirmation
}
Serial.println(); //optional: for confirmation
}
}
//Print the whole string at once - will be performed only if thers is data inside it
//you must supply the array variable
void printSerialString(char *strArray) {
int i=0;
if (strArray[i] != 0) {
while(strArray[i] != 0) {
Serial.print( strArray[i] );
strArray[i] = 0; // optional: flush the content
i++;
}
}
}
//utility function to know wither an array is empty or not
boolean isStringEmpty(char *strArray) {
if (strArray[0] == 0) {
return true;
} else {
return false;
}
}
void setup() {
Serial.begin(9600);
}
void loop () {
//simple feedback from Arduino
Serial.println("Hello World");
//read the serial port and create a string out of what you read
readSerialString(serInString);
//do somenthing else perhaps wait for other data or read another Serial string
Serial.println("------------ arduino is doing somenthing else ");
if( isStringEmpty(serInString) == false) { //this check is optional
Serial.println("Arduino recorded that you said: ");
//try to print out collected information. it will do it only if there actually is some info.
printSerialString(serInString);
Serial.println();
}
Serial.println();
//slows down the visualization in the terminal
delay(2000);
}

View File

@ -1,43 +0,0 @@
/* Serial Read Advanced
* --------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The LED will light up when receiving a 'H' over the serial
* port. The LED will blink shortly.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the data from the serial port
int serbyte = 0; // variable to store the VALID data from the port
void setup() {
pinMode(ledPin,OUTPUT); // declare the LED's pin as output
Serial.begin(9600); // connect to the serial port
}
void loop () {
// read the serial port
serbyte = Serial.read();
// if the input is '-1' then there is no data
// at the input, otherwise store it
if (serbyte != -1) {
val = serbyte;
}
// if the stored value is 'H' turn the LED on
if (val == 'H') {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(200);
}

View File

@ -1,35 +0,0 @@
/* Serial Read Basic
* -----------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The LED will light up when receiving a 'H' over the serial
* port. The LED will blink shortly.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the data from the serial port
void setup() {
pinMode(ledPin,OUTPUT); // declare the LED's pin as output
Serial.begin(9600); // connect to the serial port
}
void loop () {
// read the serial port
val = Serial.read();
// if the input is '-1' then there is no data
// at the input, otherwise check out if it is 'H'
if (val != -1) {
if (val == 'H') {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
}
}
}

View File

@ -1,40 +0,0 @@
/* Serial Write Basic
* ------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The LED will light up when pressing a button. At the same
* time, Arduino will send two different strings over the serial
* port depending if the button is pressed or released.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/
int ledPin = 13; // select the pin for the LED
int buttonPin = 2; // select the pin for the button
int val = 0; // variable to store the data from the serial port
void setup() {
pinMode(ledPin,OUTPUT); // declare the LED's pin as output
pinMode(buttonPin, INPUT); // delcare the button pin as input
Serial.begin(9600); // connect to the serial port
}
void loop () {
// read the button and store the value
val = digitalRead(buttonPin);
// if the button is at HIGH, turn the LED on, off otherwise
if (val == HIGH) {
Serial.print("HIGH");
digitalWrite(ledPin, HIGH);
} else {
Serial.print("LOW");
digitalWrite(ledPin, LOW);
}
Serial.println();
delay(1000); // convenient to use delays when sending stuff back to the comp.
}