// Adam Randall - Random Numbers Program, generates 100 random numbers between 1 and 10
long randNumber;
void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
for (int i=0; i<100; i++){
randNumber = random(0, 10);
Serial.println(randNumber);
delay(500);
}
}
void loop() {
}
Thursday, March 11, 2010
Sunday, March 7, 2010
Task 29 - Resources
http://http//webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/index.html
This website has some great arduino projects and has pictures of the arduino setup and the circuit board, so the user can create it.
http://http//www.practicalarduino.com/news/id/461
The above link is to website about an Arduino book called: Pratical Arduino. Looks like it would be a good book to own to make some interesting projects with.
http://http://falconphysics.blogspot.com/2009/01/arduino-in-high-school-electronics.html
This website is a techers blog about teaching and using the arduinos. It seems like a very cool idea and I wish this happened in my highschool. Even the video of the robot is cool.
This website has some great arduino projects and has pictures of the arduino setup and the circuit board, so the user can create it.
http://http//www.practicalarduino.com/news/id/461
The above link is to website about an Arduino book called: Pratical Arduino. Looks like it would be a good book to own to make some interesting projects with.
http://http://falconphysics.blogspot.com/2009/01/arduino-in-high-school-electronics.html
This website is a techers blog about teaching and using the arduinos. It seems like a very cool idea and I wish this happened in my highschool. Even the video of the robot is cool.
Friday, March 5, 2010
LED Project - Assignment 1
// Melody by Adam Randall
// This is a program designed to output LED's when notes are played
// through a speaker. Below is the code used.
int speakerPin = 12; // output Speaker
int led1 = 9; // output LED1
int led2 = 8; // output LED2
int led3 = 7; // output LED3
int led4 = 6; // output LED4
int led5 = 5; // output LED5
int led6 = 4; // output LED6
int length = 24; // the number of notes
char notes[] = "ggcGfcg ggcGfcg bcAcAbA "; // a space represents a rest
int beats[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH); // speaker on
delayMicroseconds(tone); // delay
digitalWrite(speakerPin, LOW); // spaker off
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = {'d', 'e', 'f', 'g', 'a', 'b', 'c', 'G', 'A'}; // notes
int tones[] = {587, 659, 698, 784, 880, 988, 1047, 831, 932 }; // frequency
if (note == 'c') {
digitalWrite (led1, HIGH); // LED1 on
delay (50); //delay
digitalWrite (led1, LOW); // LED1 off
}
else if (note == 'g') {
digitalWrite (led2, HIGH); // LED2 on
delay (50); //delay
digitalWrite (led2, LOW); // LED2 off
}
else if (note == 'G') {
digitalWrite (led3, HIGH); // LED3 on
delay (50); //delay
digitalWrite (led3, LOW); // LED3 off
}
else if (note == 'b') {
digitalWrite (led4, HIGH); // LED4 on
delay (50); //delay
digitalWrite (led4, LOW); // LED4 off
}
else if (note == 'A') {
digitalWrite (led5, HIGH); // LED5 on
delay (50); //delay
digitalWrite (led5, LOW); // LED5 off
}
else if (note == 'f') {
digitalWrite (led6, HIGH); // LED6 on
delay (50); //delay
digitalWrite (led6, LOW); // LED6 off
}
// play the tone corresponding to the note name
for (int i = 0; i < 20; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
Below is a video of th assignment at work:
Task 32 - Push Button Project + Serial output stating the condition of LED and Button.
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 on:
digitalWrite(ledPin, HIGH);
Serial.println("Button Pressed, LED on");//
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.println("Button Pressed, LED off");
}
}
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 on:
digitalWrite(ledPin, HIGH);
Serial.println("Button Pressed, LED on");//
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.println("Button Pressed, LED off");
}
}
Task 31 - Reversed Push Button Project
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);
}
}
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);
}
}
Thursday, March 4, 2010
Task 30 - Push Button Project
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);
}
}
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);
}
}
Key Words Glossary
setup() - Used to initialised varibles as the start of a program eg pinMode(buttonPin, INPUT);
loop() - Loops anything you state on the board eg void loop()
{
if (digitalRead(buttonPin) == HIGH)
serialWrite('H');
else
serialWrite('L');
delay(1000);
}
digitalWrite - DigitalWrite is used to output something such as an LED as HIGH (on) or LOW (off) eg digitalWrite(ledPin, HIGH);
analogRead()- Reads the value from the specified analog pin eg int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
Serial.print()- Prints data to the serial port as human-readable ASCII text. eg •Serial.print("Hello world.") gives "Hello world."
Serial.println()- Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). eg Serial.println("Hello world.") followed by a new line
pinMode()- Configures the specified pin to behave either as an input or an output. eg pinMode(ledPin, OUTPUT); // sets the digital pin as output
digitalRead() - Reads the value from a specified digital pin, either HIGH or LOW. eg
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
loop() - Loops anything you state on the board eg void loop()
{
if (digitalRead(buttonPin) == HIGH)
serialWrite('H');
else
serialWrite('L');
delay(1000);
}
digitalWrite - DigitalWrite is used to output something such as an LED as HIGH (on) or LOW (off) eg digitalWrite(ledPin, HIGH);
analogRead()- Reads the value from the specified analog pin eg int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
Serial.print()- Prints data to the serial port as human-readable ASCII text. eg •Serial.print("Hello world.") gives "Hello world."
Serial.println()- Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). eg Serial.println("Hello world.") followed by a new line
pinMode()- Configures the specified pin to behave either as an input or an output. eg pinMode(ledPin, OUTPUT); // sets the digital pin as output
digitalRead() - Reads the value from a specified digital pin, either HIGH or LOW. eg
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
Subscribe to:
Posts (Atom)