Getting started with arduino

Ok!, these are my first exercises for this seminar. I followed the directions and I did some exercises. Here they are:

Digital In: Button

First exercise with the Arduino board using a button to turn on and off the led that is on Pin 13.

Materials:
10 KΩ resistor
button

You can also see this code if you open the Button file in your Arduino Application.

 
int inputPin = 7;
int pinVal = 0;
int ledPin = 13;

void setup(){
pinMode(inputPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop(){

pinVal = digitalRead(inputPin);
if (pinVal == HIGH){
Serial.println("Hight");
digitalWrite(ledPin, HIGH);
}else{
Serial.println("Low");
digitalWrite(ledPin, LOW);
}
}

Analog In: Potentiometer

Second exercise done for cheking how the analog input works.

Materials:
Potentiometer

You can also see this code if you open the Analoginput file in your Arduino Application. I added serial output to see what was the information sent by the potentiometer.

 
/*
* AnalogInput
* by DojoDave
*
* 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
Serial.begin(9600);
}

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
Serial.println(val);
}

Here I use the same Sketch but a different circuit. We just changed the potentiometer for a light sensor.
First I used a 10 KΩ resistor, and the input values from the sensor were very high, so I put a 220 Ω resistor in order to get lower values.

Analog sensor In, digital light Out:

Finally I put a LED that turns on when there is no light over the sensor.

While I was working on this exercise I realized that I could make a Theremin-music sequencer using several light sensor and several LEDs. I think I will try to do it for tomorrow.

 
int potPin = 2; // select the input pin for the sensor
int ledPin01 = 13; // select the pin for one LED
int ledPin02 = 7; // select the pin for another LED
int val = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin01, OUTPUT); // declare the ledPin01 as an OUTPUT
pinMode(ledPin02, OUTPUT); // declare the ledPin02 as an OUTPUT
Serial.begin(9600);
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
Serial.println(val); // print the value from the sensor on the serial terminal.

if(val<30){
digitalWrite(ledPin01, HIGH); // turn the ledPin01 on
digitalWrite(ledPin02, HIGH); // turn the ledPin02 on
}else{
digitalWrite(ledPin01, LOW); // turn the ledPin01 off
digitalWrite(ledPin02, LOW); // turn the ledPin02 off
}
}