LuckyTest

Description

In this tutorial is shown how to use the sensors and the actuators of the Arduino Lucky Shield.
In particular this sketch allows to show a message on an Oled display and to print on the Serial Monitor the measured values of temperature, pressure, altitude, humidity, aceleration and the status of Leds, Relays and Joystick integrated on the shield.


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 into the IDE.

Hardware 

Circuit

Mount the Lucky Shield on an Arduino board.
After plug the Oled display on the shield and finally connect the board to PC using a USB cable.
Lucky shield and oled

Code

Now upload the code below, using the Arduino IDE:

/*
test_lucky

A simple Arduino sketch that reads data from the on-board sensors of the Lucky Shield and checks all gpios.
It prints sensors data via the Serial Monitor.

The following pin are defined in the Lucky Shield library and directly map the digitals
of the Lucky Shield.

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

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


//set the sea level pressure
#define SEALEVELPRESSURE_HPA 1008

void setup() {

lucky.begin();
Serial.begin(9600);
while(!Serial);
}

void loop() {

//print on the oled
lucky.oled().setTextSize(2);
lucky.oled().setTextColor(WHITE);
lucky.oled().setCursor(3,23);
lucky.oled().clearDisplay();
lucky.oled().print("Arduino");
lucky.oled().display();

//write gpio
lucky.gpio().digitalWrite(LED1,HIGH);
lucky.gpio().digitalWrite(REL1,HIGH);
lucky.gpio().digitalWrite(LED2,LOW);
lucky.gpio().digitalWrite(REL2,LOW);
delay(1000);
lucky.gpio().digitalWrite(LED2,HIGH);
lucky.gpio().digitalWrite(REL2,HIGH);
lucky.gpio().digitalWrite(LED1,LOW);
lucky.gpio().digitalWrite(REL1,LOW);
delay(1000);

//read gpios value
Serial.print("LED1: ");
Serial.print(lucky.gpio().digitalRead(LED1));
Serial.print(" LED2: ");
Serial.print(lucky.gpio().digitalRead(LED2));
Serial.print(" REL1: ");
Serial.print(lucky.gpio().digitalRead(REL1));
Serial.print(" REL2: ");
Serial.println(lucky.gpio().digitalRead(REL2));
Serial.print(" joy-D: ");
Serial.print(lucky.gpio().digitalRead(JOYD));
Serial.print(" joy-R: ");
Serial.print(lucky.gpio().digitalRead(JOYR));
Serial.print(" joy-L: ");
Serial.print(lucky.gpio().digitalRead(JOYL));
Serial.print(" joy-U: ");
Serial.print(lucky.gpio().digitalRead(JOYU));
Serial.print(" joy-C: ");
Serial.println(lucky.gpio().digitalRead(JOYC));

//read accelerometer sensor
lucky.accelerometer().read();
Serial.print("x: ");
Serial.print(lucky.accelerometer().x());
Serial.print(" y: ");
Serial.print(lucky.accelerometer().y());
Serial.print(" z: ");
Serial.println(lucky.accelerometer().z());

//read magnetometer sensor
lucky.magnetometer().read();
Serial.print("Mx: ");
Serial.print(lucky.magnetometer().x());
Serial.print(" My: ");
Serial.print(lucky.magnetometer().y());
Serial.print(" Mz: ");
Serial.println(lucky.magnetometer().z());


//read environment sensor
Serial.print("Temperature = ");
Serial.print(lucky.environment().temperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(lucky.environment().pressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(lucky.environment().altitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(lucky.environment().humidity());
Serial.println(" %");

//clear oled
lucky.oled().clearDisplay();
lucky.oled().display();

delay(1000);

}

Output

This sketch tests all sensors and actuators of the Lucky shield.
On the Display is shown the word "Arduino":
Oled test sketch
Open the Serial Monitor to read the sensor values and the status of Relays, Leds and Joystick:

serial monitor test sketch