Friday, March 19, 2010

programmes

7. Blink: Blinks on for one second and off for one second continuously.

int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}


//=================================================================================



8. Quick Blink: Turns on for 10ms and off for one second continuously.

int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(10); // wait for 10ms
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}


//=================================================================================



9. Slow Blink: turns on for one second and off for 10ms continuously.

int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(10); // wait for 10ms
}


//=================================================================================


10. Rapid Blink: The LED blinks rapidly turning off and on every 10ms.

int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(10); // wait for 10ms
digitalWrite(ledPin, LOW); // set the LED off
delay(10); // wait for 10ms
}

My cut off point is three. I think it would differ from person to person seen as some people have better eye sight than others.


//=================================================================================

12. alternate blink



int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
digitalWrite(redLedPin, LOW); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
digitalWrite(redLedPin, HIGH); // set the LED off
delay(del); // wait for a second
stop;
}


//=================================================================================


13.mostly on/off

int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 250;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH);// set the LED on
digitalWrite(redLedPin, LOW); // set the LED on
delay(subdel); // wait for a second
digitalWrite(ledPin, LOW);// set the LED on
delay(50);
digitalWrite(ledPin, HIGH);// set the LED on
delay(subdel);
digitalWrite(ledPin, LOW); // set the LED off
digitalWrite(redLedPin, HIGH); // set the LED off
delay(del); // wait for a second

}


//=================================================================================


16.blink blink, wink

int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH);// set the LED on
digitalWrite(redLedPin, LOW); // set the LED on
delay(subdel); // wait for a second
digitalWrite(ledPin, LOW);// set the LED on
delay(50);
digitalWrite(ledPin, HIGH);// set the LED on
delay(subdel);
digitalWrite(ledPin, LOW); // set the LED off
delay(subdel); // wait for a second
digitalWrite(redLedPin, HIGH); // set the LED off
delay(20); // wait for a second
digitalWrite(redLedPin, LOW); // set the LED off
delay(subdel); // wait for a second

}


//=================================================================================


17. blinks 5 times


int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, LOW); // set the LED on
for (int i = 0; i <>

//=================================================================================


18. blink random amount


int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
long rand = random(1,10);
digitalWrite(ledPin, LOW); // set the LED on
for (int i = 0; i <>

//=================================================================================


19. increase rate of blinking


int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
long rand = random(1,10);
digitalWrite(ledPin, LOW); // set the LED on
for (int i = 0; i <>

//=================================================================================


20. increase then decrease rate of blinking


int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
int subdel = 500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
int i = 0;
long rand = random(1,10);
digitalWrite(ledPin, LOW); // set the LED on

while (i < i =" 0;">

//=================================================================================



21. output word, "blink"


int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
Serial.println("blink");
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200); // wait for a second
}


//=================================================================================



22. output colour of LED that is on


*/

int redLedPin = 13;// LED connected to digital pin 13
int yellowLedPin = 12;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(redLedPin, HIGH); // set the LED on
Serial.println("red");
delay(200); // wait for a second
digitalWrite(redLedPin, LOW); // set the LED off
delay(200); // wait for a second

digitalWrite(yellowLedPin, HIGH); // set the LED on
Serial.println("yellow");
delay(200); // wait for a second
digitalWrite(yellowLedPin, LOW);// set the LED off
delay(200); // wait for a second
}


//=================================================================================


30. Button push

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// 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

// variables will change:
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);
}

void loop(){
// 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);
}
}

//=================================================================================

31. reverse button

// 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

// variables will change:
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);
}

void loop(){
// 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 off:
digitalWrite(ledPin, LOW);
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}

//=================================================================================

32. reverse button, showing text

// 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

// variables will change:
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);
Serial.begin(9600);
}

void loop(){
// 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 off:
digitalWrite(ledPin, LOW);
Serial.println("button has been pushed, LED is off");
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}

//=========================================

34. 100 random numbers between 0 and 9

//creates 100 random numbers between 0 and 9 and outputs them
//to the screen.

const int maxNumber = 100;
const int maxValue = 8;
const int minValue = 0;
int randomNumber;

// The setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
for (int i = 0; i < randomnumber =" random(minValue,">
//=========================================

35. 100 random numbers between 0 and 9
in listed order

//creates 100 random numbers between 0 and 9 and outputs them
//to the screen.

const int maxNumber = 100;
const int maxValue = 8;
const int minValue = 1;
int randomNumber;


// The setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
for (int i = 0; i < randomnumber =" random(minValue," size="3">//=========================================

36. 100 random numbers between 0 and 9
in listed order with average

//creates 100 random numbers between 0 and 9 and outputs them
//to the screen.

const int maxNumber = 10000;
const int maxValue = 8;
const int minValue = 1;
int randomNumber;
int timeDelay;


// The setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
Serial.flush();
float sum = 0;
float average = 0;
for (int i = 0; i < randomnumber =" random(minValue," average =" sum/maxNumber;" size="3">//=========================================

37. baud rates

min - 4800
max - 19200


//=========================================

38. reading LDR with button


int buttonPin2 = 2;
int photocellPin = 0;
int photoCellReading;
int buttonState;

// The setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(buttonPin2, INPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH)
{
photoCellReading = analogRead(photocellPin);
Serial.println(photoCellReading);
delay(1000);
}

}

//=========================================

39. reading LDR with button
and listed results

int buttonPin2 = 2;
int photocellPin = 0;
int photoCellReading;
int buttonState;
int buttonPushCount = 0;

// The setup() method runs once, when the sketch starts
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(buttonPin2, INPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{

buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH)
{
buttonPushCount ++;
photoCellReading = analogRead(photocellPin);
Serial.print("reading number ");
Serial.print(buttonPushCount);
Serial.print(" = ");
Serial.println(photoCellReading);
delay(1000);
}

}

//=========================================

54. data logging, no array or eeprom

int a = 0;
int value;
int readPin = 0;
boolean run = true;

void setup()
{
Serial.begin(9600);
pinMode(readPin, INPUT);

}

void loop()
{

readData();

while(run)
{

}
}
void readData()
{

for (int i = 0; i < value =" analogRead(readPin);" a =" a" a =" 0;" readpin =" 0;" run =" true;" i =" 0;" value =" intArray[i];" a =" a" i =" 0;" currvalue =" analogRead(readPin);">

int a = 1;
int value;
int readPin = 0;
boolean run = true;
void setup()
{
Serial.begin(9600);
pinMode(readPin, INPUT);

}

void loop()
{
storeData();
readData();

while(run)
{

}
}
void readData()
{

for (int i = 1; i <= 10; i++) { value = EEPROM.read(a); Serial.print(a); Serial.print("\t"); Serial.print(value); Serial.println(); a = a + 1; delay(500); } } void storeData() { for(int i = 1; i<=10; i++) { int currValue = analogRead(readPin); EEPROM.write(i, currValue); } delay(5000); } //========================================= 57. data logging with a "smallest value" function #include

int a = 1;
int value;
int readPin = 0;
boolean run = true;
void setup()
{
Serial.begin(9600);
pinMode(readPin, INPUT);

}

void loop()
{
storeData();
readData();
Serial.print("smallest value = ");
Serial.print(findSmallest());

while(run)
{

}
}
void readData()
{

for (int i = 1; i <= 10; i++) { value = EEPROM.read(a); Serial.print(a); Serial.print("\t"); Serial.print(value); Serial.println(); a = a + 1; delay(500); } } void storeData() { for(int i = 1; i<=10; i++) { int currValue = analogRead(readPin); EEPROM.write(i, currValue); } delay(5000); } int findSmallest() { int smallestValue = EEPROM.read(1); for(int i = 1; i <= 10; i++) { int tempValue = EEPROM.read(i); if (tempValue < smallestvalue =" tempValue;">

No comments:

Post a Comment