BrickHardwareYourSoftware

From ArduinoInfo
Jump to navigation Jump to search

Brick Hardware - Your Software


OK, we're starting here:


You are about to start to connect hardware and write software of your own. We'll do one example here, and then point to pages with details of each of the Input and Output Devices included in the "Electronic Brick Starter Set".

First, save your own version of the Blink software Sketch. Click File > Save As (A box should pop up). Enter a name like "YourInitials_blink". Something like "TK_blink" perhaps. NOTE: you can use the "underline" character in software sketch names, but not most other punctuation. You should see that new name in the top Blue bar of the Arduino IDE window.

NOTE: Here, we assume you've done some experimentation with changing an Arduino software Sketch on the page called "http://arduino-info.wikispaces.com http://arduinoinfo.mywikis.net/wikiGettingStarted-Software GettingStarted-Software]", or you're already familiar with the "Arduino IDE". If not, it may be best to jump over there and then return here.

OK, let's start adding devices to your Arduino. You already have one working OutPut Device: The LED blinking on I/O Pin 13.

The simplest Input Device is a switch. Hook one up.

  • Get the "Big Pushbutton" switch and a 3-wire cable.
  • Plug the "latched" end of the cable into the Switch Brick (Only goes 1 way)
  • Plug the flat end of the cable into "I/O Pin 7" (See http://arduino-info.wikispaces.com http://arduinoinfo.mywikis.net/wikiSensorShield Sensor Shield])
  • NOTE: The 3 pins are labelled " G, V and S" for "Ground", "Vcc +5V" and "Signal"
  • Plug the cable with Black at top (G), Red in center (V) and White at bottom (S).


Now, load a new Software Sketch that will sense if the switch button is pushed, and blink the LED if it is. You can copy the example below and paste it into a new Arduino IDE window, which you get by clicking File > New in the IDE.

  • Click "Verify" external image ArduinoIDE-VerifyButton.jpg to make sure it was copied OK.
  • Then click "Upload" external image ArduinoIDE-Upload-Button.jpg and wait for "Done Uploading" message.
  • Press the button. The LED should blink while you hold the button on.


EXAMPLE SOFTWARE SKETCH:


/* YourDuino Switch-LED Example
 - Reads Switch, BLINKS LED When pressed 
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define LED     13
#define SWITCH  7

/*-----( Declare Variables )-----*/
int SwitchValue;

void setup()   /*------ SETUP: RUNS ONCE ------*/
{
  pinMode(LED, OUTPUT);
  pinMode(SWITCH, INPUT);
}/* --(end setup )--- */

void loop()   /*---- LOOP: RUNS CONSTANTLY ----*/
{
  SwitchValue = digitalRead(SWITCH);

  if (SwitchValue == HIGH)
  {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(200);
  }
}/* --(end main loop )-- */





DISCUSSION ABOUT EXAMPLE SKETCH:


For this first example we will look in detail at how the Software Sketch works and what the statements mean. Let's look at one section at a time. Soon you'll start with a almost-blank template and write your own Software Sketches.


Declare Constants:

This is where you should give easy-to-remember names to numbers that will be used in other places. This is most often used for I/O Pin numbers. To use a different pin, you only have to change your software in ONE place.

Here, you decided to connect the LED to Pin 13 and the Switch to Pin 7, so any time the letters "LED" appear, the number 13 will be used. And any time the letters "SWITCH" appear, the number 7 will be used.

/*-----( Declare Constants )-----*/
#define LED 13
#define SWITCH 7



Declare Variables:

Variables are places in memory than can hold values. The values can change as your software runs. Here, the value (SwitchValue) that comes from your switch will be different if it is pressed or not.

/*-----( Declare Variables )-----*/
int SwitchValue;


The SETUP Section:

This section of your Software Sketch runs ONCE when Arduino starts up or is reset. Here you do actions that set things up, set starting values for variables, etc. In this example, the pinMode for the LED is set to OUTPUT and the pinMode for the SWITCH is set to INPUT.

void setup() /*------ SETUP: RUNS ONCE ------*/
{
pinMode(LED, OUTPUT);
pinMode(SWITCH, INPUT);
}/* --(end setup )--- */

The LOOP Section:

This section of your Software Sketch runs over and over again in a "Loop". Here you do the main actions of your Software Sketch that make Arduino do what you want it to do.

In this example, the instruction "digitalRead" checks to see if the switch is pressed and puts the result into your variable "SwitchValue".

Next, the instruction "if" tests the SwitchValue and if it is "HIGH" (The switch is pressed) the next 4 instructions are run. They turn the LED on (HIGH), delay, turn the LED off (LOW) and delay.

And that's it! This section "Loops" over and over again, checking that switch and blinking the LED if it is pressed.

void loop() /*---- LOOP: RUNS CONSTANTLY ----*/
{
SwitchValue = digitalRead(SWITCH);

if (SwitchValue == HIGH)
{
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(200);
}
}/* --(end main loop )-- */


Try a few changes. Each time, click Upload to load your new changed program into Arduino. How about these???

  • Change the number in the "delay" instructions to longer or shorter. Make the ON time be very short, for example.
  • Change the 'if' test to LOW. The LED should blink by itself and go off when the switch is pressed.
  • Add the Buzzer output device to pin 8, set it's pinMode in SETUP, do a different digitalWrite to it in LOOP. Maybe the same as the LED. Maybe different.


Next: We will look at separate pages for the rest of the Input Devices in the "http://arduino-info.wikispaces.com http://arduinoinfo.mywikis.net/wikiBrick-StarterSetHowTo Brick-StarterSetHowTo]