19
Sep 12

Chapter 11 – Experiment 04 – TEMPERATURE SENSOR

int pin = 0; // analog pin
int tempc = 0; // temperature variables
int samples[8]; // variables to make a better precision
int i;

void setup()
{
for (i=2; i<8; i++) {
pinMode(i, OUTPUT);
}
}

void loop()
{
for(i=0; i<=7; i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(100);
}

tempc = tempc/8.0; // better precision

// fiki te gjitha llambushkat
for (i=2; i<10; i++) {
digitalWrite(2, HIGH);
}

if (tempc > 20) {
digitalWrite(3, HIGH);
}
if (tempc > 30) {
digitalWrite(4, HIGH);
}
if (tempc > 40) {
digitalWrite(5, HIGH);
}
if (tempc > 50) {
digitalWrite(6, HIGH);
}
if (tempc > 60) {
digitalWrite(7, HIGH);
}
delay(1000); // delay before loop
}


19
Sep 12

Chapter 12 – Experiment 05 – PUSH BUTTON

Up to this point we have focused entirely on outputs, time to get our microcontroller to listen, watch and feel. We’ll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because the microcontroller doesn’t sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the microcontroller pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the microcontroller to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.

experiment-pushbutton

schema-pushbutton

 

/*
Button

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

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 12.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
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 =  12;      // 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);

digitalWrite(ledPin, HIGH);

}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

Serial.println(buttonState);

// 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);
}

delay(200);
}

 

 

MAKING IT BETTER

Making a morse code generator

Try to use the previous “traffic lights” code to create a morse code generator.


18
Sep 12

Chapter 13 – Experiment 06 – INTEGRATED CIRCUIT

Time to start playing with chips, or integrated circuits (ICs) as they
like to be called. The external packaging of a chip can be very
deceptive. For example, the chip on the Microcontroller board (a
microcontroller) and the one we will use in this circuit (a shift
register) look very similar but are in fact rather different. The price
of the Microcontroller chip on the board is a few dollars while the
74HC595 is a couple dozen cents. It’s a good introductory chip, and once
you’re comfortable playing around with it and its datasheet the world of
chips will be your oyster. The shift register (also called a serial to
parallel converter), will give you an additional 8 outputs (to control
LEDs and the like) using only three Microcontroller pins. They can also be
linked together to give you a nearly unlimited number of outputs using
the same four pins. To use it you “clock in” the data and then lock it
in (latch it). To do this you set the data pin to either HIGH or LOW,
pulse the clock, then set the data pin again and pulse the clock
repeating until you have shifted out 8 bits of data. Then you pulse the
latch and the 8 bits are transferred to the shift registers pins. It
sounds complicated but is really simple once you get the hang of it.

integrated-circuit

integrated-circuit

Code

/*     ———————————————————
*     |  Arduino Experimentation Kit Example Code             |
*     |  CIRC-02 .: 8 LED Fun :. (Multiple LEDs)   |
*     ———————————————————
*
*  A few Simple LED animations
*
* For more information on this circuit http://tinyurl.com/d2hrud
*
*/

//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[7] would equal 9

/*
* setup() – this function runs once when you turn your Arduino on
* We the three control pins to outputs
*/
void setup()
{

//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 8; i++){         //this is a loop and will repeat eight times
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
}                                   //the code this replaces is below

/* (commented code will not run)
* these are the lines replaced by the for loop above they do exactly the
* same thing the one above just uses less typing
pinMode(ledPins[0],OUTPUT);
pinMode(ledPins[1],OUTPUT);
pinMode(ledPins[2],OUTPUT);
pinMode(ledPins[3],OUTPUT);
pinMode(ledPins[4],OUTPUT);
pinMode(ledPins[5],OUTPUT);
pinMode(ledPins[6],OUTPUT);
pinMode(ledPins[7],OUTPUT);
(end of commented code)*/
}

/*
* loop() – this function will start after setup finishes and then repeat
* we call a function called oneAfterAnother(). if you would like a different behaviour
* uncomment (delete the two slashes) one of the other lines
*/
void loop()                     // run over and over again
{
oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off
//oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with
//much less typing
//oneOnAtATime();          //this will turn one LED on then turn the next one
//on turning the
//former off (one LED will look like it is scrolling
//along the line
//inAndOut();              //lights the two middle LEDs then moves them out then back
//in again
}

/*
* oneAfterAnotherNoLoop() – Will light one LED then delay for delayTime then light
* the next LED until all LEDs are on it will then turn them off one after another
*
* this does it without using a loop which makes for a lot of typing.
* oneOnAtATimeLoop() does exactly the same thing with less typing
*/
void oneAfterAnotherNoLoop(){
int delayTime = 100; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (connected to pin 2 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (connected to pin 3 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (connected to pin 4 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (connected to pin 5 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (connected to pin 6 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (connected to pin 7 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (connected to pin 8 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (connected to pin 9 )
delay(delayTime);                //waits delayTime milliseconds

//Turns Each LED Off
digitalWrite(ledPins[7], LOW);  //Turns on LED #0 (connected to pin 2 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[6], LOW);  //Turns on LED #1 (connected to pin 3 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[5], LOW);  //Turns on LED #2 (connected to pin 4 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[4], LOW);  //Turns on LED #3 (connected to pin 5 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[3], LOW);  //Turns on LED #4 (connected to pin 6 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[2], LOW);  //Turns on LED #5 (connected to pin 7 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[1], LOW);  //Turns on LED #6 (connected to pin 8 )
delay(delayTime);                //waits delayTime milliseconds
digitalWrite(ledPins[0], LOW);  //Turns on LED #7 (connected to pin 9 )
delay(delayTime);                //waits delayTime milliseconds
}

/*
* oneAfterAnotherLoop() – Will light one LED then delay for delayTime then light
* the next LED until all LEDs are on it will then turn them off one after another
*
* this does it using a loop which makes for a lot less typing.
* than oneOnAtATimeNoLoop() does exactly the same thing with less typing
*/
void oneAfterAnotherLoop(){
int delayTime = 100; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower

//Turn Each LED on one after another
for(int i = 0; i <= 7; i++){
digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
delay(delayTime);                //gets one added to it so this will repeat
}                                  //8 times the first time i will = 0 the final
//time i will equal 7;

//Turn Each LED off one after another
for(int i = 7; i >= 0; i–){  //same as above but rather than starting at 0 and counting u
//p
//we start at seven and count down
digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i
delay(delayTime);                //gets one subtracted from it so this will repeat
}                                  //8 times the first time i will = 7 the final
//time it will equal 0

}

/*
* oneOnAtATime() – Will light one LED then the next turning off all the others
*/
void oneOnAtATime(){
int delayTime = 100; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower

for(int i = 0; i <= 7; i++){
int offLED = i – 1;  //Calculate which LED was turned on last time through
if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 7;        //turn on LED 2 and off LED 1)
}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)
//instead we turn off LED 7, (looping around)
digitalWrite(ledPins[i], HIGH);     //turn on LED #i
digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
delay(delayTime);
}
}

/*
* inAndOut() – This will turn on the two middle LEDs then the next two out
* making an in and out look
*/
void inAndOut(){
int delayTime = 100; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower

//runs the LEDs out from the middle
for(int i = 0; i <= 3; i++){
int offLED = i – 1;  //Calculate which LED was turned on last time through
if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 3;        //turn on LED 2 and off LED 1)
}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)
//instead we turn off LED 7, (looping around)
int onLED1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
//
//#0 when i = 3
int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
//
//#7 when i = 3
int offLED1 = 3 – offLED; //turns off the LED we turned on last time
int offLED2 = 4 + offLED; //turns off the LED we turned on last time

digitalWrite(ledPins[onLED1], HIGH);
digitalWrite(ledPins[onLED2], HIGH);
digitalWrite(ledPins[offLED1], LOW);
digitalWrite(ledPins[offLED2], LOW);
delay(delayTime);
}

//runs the LEDs into the middle
for(int i = 3; i >= 0; i–){
int offLED = i + 1;  //Calculate which LED was turned on last time through
if(i == 3) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 0;        //turn on LED 2 and off LED 1)
}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)
//instead we turn off LED 7, (looping around)
int onLED1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
//
//#0 when i = 3
int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
//
//#7 when i = 3
int offLED1 = 3 – offLED; //turns off the LED we turned on last time
int offLED2 = 4 + offLED; //turns off the LED we turned on last time

digitalWrite(ledPins[onLED1], HIGH);
digitalWrite(ledPins[onLED2], HIGH);
digitalWrite(ledPins[offLED1], LOW);
digitalWrite(ledPins[offLED2], LOW);
delay(delayTime);
}
}


17
Sep 12

Chapter 10 – Experiment 03 – LIGHT SENSOR

In a famous film (for whom is not a geek, the film is Star Wars) they said “Luke use the source”, in this experiment we will use the source “light” to have some nice results. For the first time we will use a sensor that allows us to interact with the external environment, a small component called photo-resistor based on a light resistance, it will sense the light and will allow the microcontroller to react and change the intensity of a small LED. The photo-resistor creates a different resistance based on the intensity of the light. Changing the resistance it changes the result voltage. The microcontroller reads the different voltage and will light up the LED with more or less intensity. A low resistance value will occur when the sensor is well lit and a high resistance value will occur when it is in darkness.

The schema

int photocellPin = 7;
int photocellOutput;

int led = 12;

void setup(void)
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop(void)
{
// digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)

photocellOutput = analogRead(photocellPin);

Serial.print(“Raw data = “);
Serial.println(photocellOutput); // raw data from the photocell

if (photocellOutput < 1020) {
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
} else {
digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
}

delay(1);
}

NOT WORKING? (3 things to try)

LED Remains Dark
This is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it up and give it a twist.

It Isn’t Responding to Changes in Light.
Given that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check if it is its in the right place.

Still not really quite working?
You may be in a room which is either too bright or too dark. Try turning the lights on or off to see if this helps. Or if you have a flashlight nearby give that a try.

MAKING IT BETTER

Reverse the response:
Perhaps you would like the opposite response. Don’t worry we can easily reverse this response just change: digitalWrite(led, HIGH); to LOW and digitalWrite(led, LOW); to HIGH

Upload and watch the response change


17
Sep 12

Chapter 09 – Experiment 02 – TRAFFIC LIGHTS

In the previous experiment we had a single LED blinking, changed the fading, the brightness and the speed of blinking. With this new experiment, we will try to improve adding more LEDs and creating a more interesting application that we see every day when we drive all around, we wanted to provide you the experiment with practical application, so you can understand this kit is not only for fun but make you to understand and create also some practical applications. Now it’s time to go go ahead with this new experiment, let’s connect two groups of 3 LEDs and a group of two LEDs. The three LEDs will be our traffic lights, like in a crossing and the group of two LEDs will be the pedestrians light sign.

In addition to create a more complex program, we will learn to use arrays, a for() loop, functions and simple programming methods to a program small (this due the limited memory of the microprocessor).

CODE

/*
Traffic lights
Turns on a series of LEDs as a traffic light.

This example code is in the public domain.
*/

// a is the first traffic lights, b the second one. Red is on 7 pin, Yellow on 6 and Green on 5. For the second traffic light you have 4,3 and 2.

int a = 7; // first traffic light
int b = 4; // second traffic light

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as an output.
pinMode(a, OUTPUT);
pinMode(a – 1, OUTPUT);
pinMode(a – 2, OUTPUT);

pinMode(b, OUTPUT);
pinMode(b – 1, OUTPUT);
pinMode(b – 2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// You will notice that the multi tasking is not fully implemented. Check
trafficlight(a);
trafficlight(b);
}

void trafficlight(int a) {
led(a, HIGH, 0);
led(a – 1, LOW, 0);
led(a – 2, LOW, 0);
delay (4000);

led(a, LOW, 0);
led(a – 1, LOW, 0);
led(a – 2, HIGH, 0);
delay (4000);

led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);
led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);
led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);

}

void led(int led, int value, int ledDelay) {
digitalWrite(led, value);   // turn the LED on (HIGH is the voltage level)
delay(ledDelay);               // wait for X milliseconds
}

NOT WORKING?

Some LEDs Fail to Light
It is easy to insert an LED backwards. Check the LEDs that aren’t working and ensure they the right way around.

Operating out of sequence
With eight wires it’s easy to cross a couple. Double check that the first LED is plugged into pin 2 and each pin there after.

Starting afresh
It is easy to accidentally misplace a wire without noticing it. Pulling everything out and starting with a fresh slate is often easier than trying to track down the problem.

MAKING IT BETTER

Making the two traffic lights working in sync

The library ( http://www.arduino.cc/playground/Main/MsTimer2 ) can help to run in background tasks, try to download and use it.

Switching to loops:
In the loop() function there are 4 lines. The last three all start with a ‘//’. This means the line is treated as a comment (not run). To switch the program to use different type of animations, remove and add the // in the code leaving only one function request uncommented.

Upload the program, you will notice that using oneAfterAnotherNoLoop();  or oneAfterAnotherLoop(); functions nothing is changed. You can take a look at the two functions, each does the same thing, but use different approaches (hint: the second one uses a for loop).

The other two functions  oneOnAtATime();  and inAndOut(); are different, the first one is doing that the other one is doing this.

Extra animations:
Tired of these animations? Try to create and invent new sample animations using your fantasy. In one of the future experiments you will see how this different type of animations will allow you to create an incredible special effect. You will be amazed, we promise!

Below: amateur video showing the experiment working

 


istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat