Saturday, 21 December 2013

Getting Started with Arduino

Made these note for my grandson...

Starting with Arduino
Blink a light (LED)

Start the IDE software, click on its icon in the dock

Programming

1 Programs have two parts called 

void setup()
{
  put what we want to do here
}

and

void loop()
{
  put what we want to repeat here
}

2 setup() and loop() are functions. 

Functions are like this

return_this function(input_this)
{
... do something
return this;
}

 what they do is in the { ... } brackets. They don’t return anything back so they are void.

3 the Pins have to be told what they are, mode = INPUT or OUTPUT. 

pinMode(pin, mode);

End each line with ‘;’

4 Give a name to the PIN number and a waiting TIME between flashes

#define NAME VALUE,  for example like this

#define LEDPIN 7
5 Code now looks like this

// pin number 7 and delay time of 1sec = 1000msec
#define LEDPIN 7
#define TIME 1000

// setup pin 7 as an output
void setup() 
{                
  pinMode(LEDPIN, OUTPUT);     
}

any line starting “//“ is just a comment, not part of your program code

5 Now for the loop(), which gets repeated

This is what we have to do

- turn the LED on
- wait for 1 sec
- turn the LED off
- wait for 1 sec
... repeat...

To turn the LED on and off, we have to OUTPUT a HIIGH or LOW signal on the pin.

digitalWrite(pin, value);

and we have to create a delay

delay(1000); 

1000 is in milliseconds, 1000 msec = 1 sec.

6 The code now looks like this

// pin number 7 and delay time of 1sec = 1000msec
#define LEDPIN 7
#define TIME 1000

// setup pin 7 as an output
void setup() 
{                
  pinMode(LEDPIN, OUTPUT);     
}

// repeat the following code to flash the light
void loop() 
{
  digitalWrite(LEDPIN, HIGH);
  delay(TIME);
  digitalWrite(LEDPIN, LOW);
  delay(TIME);
}

7 Type all this in.

Then press the “tick” button to check it is right. If so press the “arrow” button to send your program to the Arduino. 

The LED will start to flash!

No comments: