Everything you make with Arduino has a code. The code is there so the computer understands what you want to do and with this code you always get two values - HIGH and LOW. High starts at 1 and will obviously increase where needed (like a range for a motion sensor - every time your hand moves towards the sensor the higher the value) and obviously low will always be 0 or something lower than high (also like the range for a motion sensor - every time you move your hand away from the motion sensor the value goes down - because the sensor can't sense anything is there)
Here are some Arduino codes that we have used on Wednesdays lessons:
-This is for the switch:
/* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
}
digitalWrite(outPin, state);
previous = reading;
}
}
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
/*// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
* ap_ReadMultiDigital_8
*
* Reads an 8 digital inputs and sends their values over the serial port.
* A byte variable is used to store the state of all eight pins. This byte
* is then sent over the serial port.
*
* This file is part of the Arduino meets Processing Project.
* For more information visit http://www.arduino.cc.
*
* copyleft 2005 by Melvin Ochsmann for Malm? University
*
*/
*
* Reads an 8 digital inputs and sends their values over the serial port.
* A byte variable is used to store the state of all eight pins. This byte
* is then sent over the serial port.
*
* This file is part of the Arduino meets Processing Project.
* For more information visit http://www.arduino.cc.
*
* copyleft 2005 by Melvin Ochsmann for Malm? University
*
*/
// 8 variables for each pin
int digitalInput_1 = 7;
int digitalInput_2 = 8;
int digitalInput_1 = 7;
int digitalInput_2 = 8;
// 8 variables to store the values
int value_1 = 0;
int value_2 = 0;
int value_1 = 0;
int value_2 = 0;
// byte variable to send state of all pins over serial port
int myByte = 0;
// control LED
int controlLED = 13;
void setup(){
int myByte = 0;
// control LED
int controlLED = 13;
void setup(){
// set pin modes
pinMode(digitalInput_1, INPUT);
pinMode(digitalInput_2, INPUT);
pinMode(digitalInput_1, INPUT);
pinMode(digitalInput_2, INPUT);
pinMode(controlLED, OUTPUT);
// begin sending out over the serial port
Serial.begin(9600);
}
Serial.begin(9600);
}
void loop(){
// set 'myByte' to zero
myByte = 0;
myByte = 0;
// then read all the INPUTS and store values in the corresponding variables
value_1 = digitalRead(digitalInput_1);
value_2 = digitalRead(digitalInput_2);
value_1 = digitalRead(digitalInput_1);
value_2 = digitalRead(digitalInput_2);
/* check values are high or low and 'add' each value to myByte
* ( what it actually does is this:
*
* 00 00 00 00 ('myByte set to zero')
* | 00 10 10 00 ('3 and 5 are 1')
* --------------
* 00 10 10 00 ('myByte after logical operation')
*
*/
* ( what it actually does is this:
*
* 00 00 00 00 ('myByte set to zero')
* | 00 10 10 00 ('3 and 5 are 1')
* --------------
* 00 10 10 00 ('myByte after logical operation')
*
*/
if (value_1) {
myByte = myByte | 1;
digitalWrite(controlLED, HIGH);
}
else
digitalWrite(controlLED, LOW);
myByte = myByte | 1;
digitalWrite(controlLED, HIGH);
}
else
digitalWrite(controlLED, LOW);
if (value_2) {
myByte = myByte | 2;
}
// send myByte out over serial port and wait a bit to not overload the port
Serial.print(myByte);
delay(10);
}
myByte = myByte | 2;
}
// send myByte out over serial port and wait a bit to not overload the port
Serial.print(myByte);
delay(10);
}
As you can see they are quite confusing, but once you get the hang of it, its actually quite easy. You just have to work hard to understand the codes....this is just for Arduino....there is Processing too!
No comments:
Post a Comment