SimpleGPIO


Description

This simple example shows how to control by the joystick the switching on/off of the Relays and LEDs integrated on Lucky Shield.
In particular:
When the joystick is moved on the LEFT, Led n1 and Relay n1 are turned on,
Instead when the joystick is moved on the RIGHT, Led n2 and Relay n2 are turned off.
Warning:
This example uses the Lucky.h library.
If you are using the Arduino IDE 1.8.x,  then download the Lucky library here and import it into the IDE.

Hardware 


Circuit

Mount the Lucky Shield on an Arduino board.
Finally connect the board to PC using a USB cable.
lucky shield circuit

Code

Now upload the code below, using the Arduino IDE:

/*
Simple GPIO

A simple Arduino sketch that turn off and on the onboard relais and leds of the Lucky Shield.
When the joystick is moved on the left, led n1 and relay n1 are turned on,
when the joystick is moved on the right, led n2 and relay n2 are turned off.

The following pins are defined in the Lucky Shield library, they directly map the digitals
of the Lucky Shield

LED1: on board first LED
LED2: on board second LED

REL1: on board first Relay
REL2: on board second Relay

JOYR: on board Joystick - moved right
JOYL: on board Joystick - moved left
JOYU: on board Joystick - moved up
JOYD: on board Joystick - moved down
JOYC: on board Joystick - central pressed

PIR: on board (Passive Infrared Sensor) Presence Sensor

PB1: on board first Push Button
PB2: on board second Push Button

created Mar 2016
by andrea@arduino.org,
sergio@arduino.org
*/

#include <Lucky.h>
#include <Wire.h>

void setup() {
lucky.begin();

}

void loop() {

//Check if the the joystick is moved to the left
if(lucky.gpio().digitalRead(JOYL) == HIGH){
lucky.gpio().digitalWrite(LED1, HIGH);
lucky.gpio().digitalWrite(REL1, HIGH);
}
else {
lucky.gpio().digitalWrite(LED1, LOW);
lucky.gpio().digitalWrite(REL1, LOW);
}

//Check if the the joystick is moved to the right
if(lucky.gpio().digitalRead(JOYR) == HIGH){
lucky.gpio().digitalWrite(LED2, HIGH);
lucky.gpio().digitalWrite(REL2, HIGH);
}
else {
lucky.gpio().digitalWrite(LED2, LOW);
lucky.gpio().digitalWrite(REL2, LOW);
}
}

OUTPUT

Now move the joystick to activate the correspondent Relay and Led.