Saltar la navegación

Ejemplos

Ejemplos de programas para llevar a cabo con la shield LENARDO o HACKATRONICS

Example Sketches:

Fuente de información: AQUÍ  y AQUÍ

1.- Push Button:

Versión 1 (No comentada)

// Comprobación de los botones de la shield LENARDO

const int buttonPin = A1; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

}

}

Pushbutton Versión 2

(Utiliza el monitor serie)

Code:

/* FILE: ARD_Multifunction_Shield_Push_Button_Example
DATE: 28/05/14
VERSION: 0.1
REVISIONS:

28/05/14 Created version 0.1

This is an example of how to use the 3 push buttons on the Hobby Components Arduino
compatible Multi Function experimenter shield (HCARDU0085).

This example sketch will continuously read the state of the 3 push buttons and
output it to the serial terminal. To see the output open up the serial
monitor (Tools-> Serial Monitor) in your Arduino development environment and make
sure the baud rate is set to 9600 Baud.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products
that directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Define pin numbers used to read the state of the buttons.
The buttons are connected to the Arduino's analogue pins but
because they only have two states we will read them as digital
inputs */


#define BUTTON1 A1
#define BUTTON2 A2
#define BUTTON3 A3

void setup()
{
Serial.begin(9600);
}

/* Main program */
void loop()
{
  /* Has button one been pressed? */
  if(!digitalRead(BUTTON1))
    /* If so then send a message to the serial port */
    Serial.println("Button 1 Pressed!");
  /* Has button two been pressed? */
  if(!digitalRead(BUTTON2))
    /* If so then send a message to the serial port */
    Serial.println("Button 2 Pressed!");
  /* Has button three been pressed? */
  if(!digitalRead(BUTTON3))
    /* If so then send a message to the serial port */
    Serial.println("Button 3 Pressed!");
}

2.- Potentiometer:

Code:

/* FILE:    ARD_Multifunction_Shield_Potentiometer_Example
   DATE:    28/05/14
   VERSION: 0.1

REVISIONS:

28/05/14 Created version 0.1

This is an example of how to read the position of the 10K potenetiometer
on the Hobby Components Arduino compatible Multi Function experimenter shield
(HCARDU0085).

The sketch will read the current position of the potentiometer and output it to
the serial terminal once every 0.5 seconds. To see the output open up the serial
monitor (Tools-> Serial Monitor) in your Arduino development environment and make
sure the baud rate is set to 9600 Baud.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Define the analogue pin used to read the potentiometer */
#define PotPin 0

void setup()
{
  Serial.begin(9600);
}

/* Main Program */
void loop()
{

  Serial.print("Potentiometer: ");
  Serial.println(analogRead(PotPin));
 
  /* Wait 0.5 seconds before reading again */
  delay(500);
}

3.- Buzzer:

Code:

/* FILE:    ARD_Multifunction_Shield_Buzzer_Example
   DATE:    28/05/14
   VERSION: 0.1

REVISIONS:
28/05/14 Created version 0.1

This is an example of how to use the buzzer on the Hobby Components Arduino compatible

Multi Function experimenter shield (HCARDU0085).

The sketch will use the buzzer to create 'pings' at a repetition rate dependant on
the current position of the 10K potentiometer.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Define the analogue pin used to read the potentiometer */
#define POT_DIO 0

/* Define the digital pin used to control the buzzer */
#define BUZZER_DIO 3

#define OFF HIGH
#define ON LOW

void setup()
{
  /* Set the buzzer pin to an output and turn the buzzer off */
  pinMode(BUZZER_DIO, OUTPUT);
  digitalWrite(BUZZER_DIO, OFF);
}

/* Main Program */
void loop()
{
  /* Read the current position of the 10K potentiometer and use it
     as a time delay */
  delay(analogRead(POT_DIO));
 
  /* Turn the buzzer on for 20ms and then turn it back off again */
  digitalWrite(BUZZER_DIO, ON);
  delay(20);
  digitalWrite(BUZZER_DIO, OFF);
}

4.- LEDS:

Code:

/* FILE:    ARD_Multifunction_Shield_LED_Example
   DATE:    28/05/14
   VERSION: 0.1
   
REVISIONS:

28/05/14 Created version 0.1

This is an example of how to use the 4 LED's on the Hobby Components Arduino
compatible Multi Function experimenter shield (HCARDU0085).

This example sketch will repeatedly turn each LED on in sequence to create a 'cylon'
effect.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products
that directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/

/* Define the DIO pin numbers for each LED */

const byte LED[] = {13,12,11,10};

void setup()
{
  /* Set each pin to outputs */
  pinMode(LED[0], OUTPUT);
  pinMode(LED[1], OUTPUT);
  pinMode(LED[2], OUTPUT);
  pinMode(LED[3], OUTPUT);
}

/* Main program */
void loop()
{
  byte Index;
 
  /* Step through each LED and turn it on in sequence */
  for(Index = 0; Index <= 3; Index++)
  {
    /* First turn all the LED's off */
    digitalWrite(LED[0], HIGH);
    digitalWrite(LED[1], HIGH);
    digitalWrite(LED[2], HIGH);
    digitalWrite(LED[3], HIGH);
    /* Then turn the next LED ON */
    digitalWrite(LED[Index], LOW);
    /* Wait a little between each sequence */
    delay(100);   
  }
 
  /* Do the same thing but in reverse order */
  for(Index = 3; Index > 0; Index--)
  {
    /* First turn all the LED's off */
    digitalWrite(LED[0], HIGH);
    digitalWrite(LED[1], HIGH);
    digitalWrite(LED[2], HIGH);
    digitalWrite(LED[3], HIGH);
    /* Then turn the next LED ON */
    digitalWrite(LED[Index], LOW);
    /* Wait a little between each sequence */
    delay(100);   
  }
}

5.- 4 Digit Seven Segment Display:

4 Digit Seven Segment Display:

Code:

/* FILE:    ARD_Multifunction_Shield_Seven_Segment_Example
   DATE:    28/05/14
   VERSION: 0.1
   
REVISIONS:

28/05/14 Created version 0.1

This is an example of how to use the 4 digit seven segment display on the Hobby
Components Arduino compatible Multi Function experimenter shield (HCARDU0085).

This example sketch will display a decimal number on the seven segment LED display
which will increment by one wever 100ms.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products
that directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Define shift register pins used for seven segment display */
#define LATCH_DIO 4
#define CLK_DIO 7
#define DATA_DIO 8
 
/* Segment byte maps for numbers 0 to 9 */
const byte SEGMENT_MAP[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90};
/* Byte maps to select digit 1 to 4 */
const byte SEGMENT_SELECT[] = {0xF1,0xF2,0xF4,0xF8};


unsigned long Cur_ms_Count; // Stores the current time in ms
unsigned long Last_ms_Count; // Stores the last time in ms the counter was last updated
int Count; // Stores the value that will be displayed

void setup ()
{
  /* Set DIO pins to outputs */
  pinMode(LATCH_DIO,OUTPUT);
  pinMode(CLK_DIO,OUTPUT);
  pinMode(DATA_DIO,OUTPUT);
 
  /* Initiliase the registers used to store the crrent time and count */
  Cur_ms_Count = millis();
  Last_ms_Count = 0;
  Count = 0;
}

/* Main program */
void loop()
{
 
  /* Get how much time has passed in milliseconds */
  Cur_ms_Count = millis();
 
  /* If 100ms has passed then add one to the counter */
  if(Cur_ms_Count - Last_ms_Count > 100)
  {

    Last_ms_Count = Cur_ms_Count;
   
    if(Count < 9999)
    {
      Count++;
    } else
    {
      Count = 0;
    }
  }
 
  /* Update the display with the current counter value */
  WriteNumber(Count);
}


/* Write a decimal number between 0 and 9999 to the display */
void WriteNumber(int Number)
{
  WriteNumberToSegment(0 , Number / 1000);
  WriteNumberToSegment(1 , (Number / 100) % 10);
  WriteNumberToSegment(2 , (Number / 10) % 10);
  WriteNumberToSegment(3 , Number % 10);
}

/* Wite a ecimal number between 0 and 9 to one of the 4 digits of the display */
void WriteNumberToSegment(byte Segment, byte Value)
{
  digitalWrite(LATCH_DIO,LOW);
  shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
  shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[Segment] );
  digitalWrite(LATCH_DIO,HIGH);    
}