Embedded device¶
Hardware overview¶
This hardware overview contains the dot segment display, ldr sensor and a green and red led (front).
This hardware overview contains the dot segment display, ldr sensor and a green and red led (back).
This hardware overview contains the dot segment display and a push button.
Wiring diagram¶
First version of my wiring diagram using the dot segment display.
In this version of my wiring diagram, the LDR sensor and 2 leds are also included.
Here I added the wiring for the push button.
The power supply is now added.
Bill of Materials¶
This is a list of all the materials I’ve used:
Part number/SKU | Manufacturer | Part name | Part description | Part quantity | Part cost | Example url |
---|---|---|---|---|---|---|
000071 | tinytronics | Breadboard 830 points | The breadboard can be combined with the Arduino | 1x | €4.00 | https://www.tinytronics.nl/shop/en/tools-and-mounting/prototyping-supplies/breadboards/breadboard-830-points |
000948 | tinytronics | LED Matrix 8*8 with MAX7219 Module - Compact | A LED Matrix of 8*8 red LEDs controlled by the MAX7219 chip. | 1x | €4.00 | https://www.tinytronics.nl/shop/nl/verlichting/matrix/led-matrix-8*8-met-max7219-module-compact |
000088 | tinytronics | DuPont Jumper wire Male-Female 20cm 10 wires | DuPont draden are used in combination with a breadboard. | 17x | €0,75 (per 10) | https://www.tinytronics.nl/shop/nl/kabels-en-connectoren/kabels-en-adapters/prototyping-draden/dupont-compatible-en-jumper/dupont-jumper-draad-male-female-20cm-10-draden |
HE0613-002 | wemos | D1 mini V2 CH340G | Microcontroller | 1x | €4.95 | https://www.hobbyelectronica.nl/product/wemos-d1-v2-usb/ |
000097 | tinytronics | Red LED - 5mm Diffuse | A red LED. | 1x | €0,10 | https://www.tinytronics.nl/shop/nl/componenten/led’s/led’s/rode-led-5mm-diffuus |
000095 | tinytronics | Green LED - 5mm Diffuse | A green LED. | 1x | €0,10 | https://www.tinytronics.nl/shop/nl/componenten/led’s/led’s/groene-led-5mm-diffuus |
000138 | tinytronics | 10kΩ resistor (standard pull-up or pull-down resistance) | A wire resistor with 5% tolerance | 4x | €0,15 | https://www.tinytronics.nl/shop/nl/componenten/weerstanden/weerstanden/10k%CF%89-weerstand-(standaard-pull-up-of-pull-down-weerstand) |
000127 | tinytronics | GL5537-1 LDR light-sensitive resistor | A light-sensitive resistor(LDR) to measure the amount of light. | 1x | €0,30 | https://www.tinytronics.nl/shop/nl/sensoren/optisch/licht-en-kleur/gl5537-ldr-lichtgevoelige-weerstand |
000140 | tinytronics | Tactile Push button switch Momentary 4pin 665mm | A push button | 1x | €0,10 | https://www.tinytronics.nl/shop/en/switches/manual-switches/pcb-switches/tactile-push-button-switch-momentary-4pin-665mm |
System requirements¶
My embedded device meets the following system requirements:
1. Embedded device sends measured sensordata to the application backend over http or https. My WeMos sends the value of the dice rolls (with the switch button as input and LED matrix as output) to the backend over http with this line in my Arduino C++ code:
String url = String(“http://866e85efa4d87f.lhr.life/insert.php?dice=”) + x;
2. Embedded device receives or retrieves status messages from the application backend. The values of the dice rolls are inserted in my backend using my insert.php file. My getdb.php takes the last 6 values and prints these in json. My app.js file then fetches this data into my dice.html, displaying the values on the page. You can see the full code of my backend here.
3. The embedded device contains at least two input sensors (e.g. LDR, buttons, joystick, capacitive touch). I’ve used an LDR to measure the values of my green and red LED and my tactile push button switch functions as an input for the LED matrix.
4. The embedded device contains at least two visual and/or sensory outputs (e.g. LED, LED Matrix, 7-segement display, motor, servo, actuator, LCD-screen, etc). I’ve used two LEDs (green and red) and a LED matrix to display the characters of the dice.
5. The embedded device uses the wifi manager for configuration of SSID, User ID (UID) en Password (PWD) for connecting to the network.* My WeMos connects to the wifi through the wifi manager using an SSID and password. You can see that here.
Arduino code¶
This is the first version of my Arduino code.
//Libraries
//Wifi library
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
//Wifi manager
#include <strings_en.h>
#include <WiFiManager.h>
//Led matrix
#include "LedControl.h"
const int ldrPin = A0;
const int ledPin = D6;
const int ledPin2 = D0;
int val = 0;
const int button = D2;
int buttonState = 0;
/*
pin D7 is connected to the DataOut
pin D5 is connected to the CLK
pin D8 is connected to CS
*/
LedControl lc=LedControl(D7,D5,D8,1);
/* we wait a bit between updates of the display */
unsigned long delaytime=500;
byte right[8]={B01000010,B01100110,B00111100,B10011001,B11000011,B01100110,B00111100,B00011000};
byte six[8]={B00000000,B11011011,B11011011,B00000000,B00000000,B11011011,B11011011,B00000000};
byte five[8]={B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
byte four[8]={B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
byte three[8]={B11000000,B11000000,B00000000,B00011000,B00011000,B00000000,B00000011,B00000011};
byte shot[8]={B00000000,B11111111,B10100111,B10100011,B10100011,B10100111,B11111111,B00000000};
byte two[8]={B00000000,B00000000,B00000000,B01100110,B01100110,B00000000,B00000000,B00000000};
byte left[8]={B00011000,B00111100,B01100110,B11000011,B10011001,B00111100,B01100110,B01000010};
byte one[8]={B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};
void setup() {
Serial.begin(115200); //Initialize serial communication
// WiFi.begin("iotroam", "7zjpji9PJn"); //SSID and password
//
// // Keep in while-loop while the device is not connected to your accesspoint.
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000); // Waiting on connection...
// }
//Set input and outputs
pinMode(button, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
randomSeed(analogRead(0));
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,5);
/* and clear the display */
lc.clearDisplay(0);
}
/*
This method will display the characters
*/
void one1() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,one[0]);
lc.setRow(0,1,one[1]);
lc.setRow(0,2,one[2]);
lc.setRow(0,3,one[3]);
lc.setRow(0,4,one[4]);
lc.setRow(0,5,one[5]);
lc.setRow(0,6,one[6]);
lc.setRow(0,7,one[7]);
delay(delaytime);
}
void left1() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,left[0]);
lc.setRow(0,1,left[1]);
lc.setRow(0,2,left[2]);
lc.setRow(0,3,left[3]);
lc.setRow(0,4,left[4]);
lc.setRow(0,5,left[5]);
lc.setRow(0,6,left[6]);
lc.setRow(0,7,left[7]);
delay(delaytime);
}
void two2() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,two[0]);
lc.setRow(0,1,two[1]);
lc.setRow(0,2,two[2]);
lc.setRow(0,3,two[3]);
lc.setRow(0,4,two[4]);
lc.setRow(0,5,two[5]);
lc.setRow(0,6,two[6]);
lc.setRow(0,7,two[7]);
delay(delaytime);
}
void shot2() {
lc.setRow(0,0,shot[0]);
lc.setRow(0,1,shot[1]);
lc.setRow(0,2,shot[2]);
lc.setRow(0,3,shot[3]);
lc.setRow(0,4,shot[4]);
lc.setRow(0,5,shot[5]);
lc.setRow(0,6,shot[6]);
lc.setRow(0,7,shot[7]);
delay(delaytime);
}
void three3() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,three[0]);
lc.setRow(0,1,three[1]);
lc.setRow(0,2,three[2]);
lc.setRow(0,3,three[3]);
lc.setRow(0,4,three[4]);
lc.setRow(0,5,three[5]);
lc.setRow(0,6,three[6]);
lc.setRow(0,7,three[7]);
delay(delaytime);
}
void four4() {
/* here is the data for the characters */
/* now display them four by four with a small delay */
lc.setRow(0,0,four[0]);
lc.setRow(0,1,four[1]);
lc.setRow(0,2,four[2]);
lc.setRow(0,3,four[3]);
lc.setRow(0,4,four[4]);
lc.setRow(0,5,four[5]);
lc.setRow(0,6,four[6]);
lc.setRow(0,7,four[7]);
delay(delaytime);
}
void five5() {
/* here is the data for the characters */
/* now display them five by five with a small delay */
lc.setRow(0,0,five[0]);
lc.setRow(0,1,five[1]);
lc.setRow(0,2,five[2]);
lc.setRow(0,3,five[3]);
lc.setRow(0,4,five[4]);
lc.setRow(0,5,five[5]);
lc.setRow(0,6,five[6]);
lc.setRow(0,7,five[7]);
delay(delaytime);
}
void six6() {
/* here is the data for the characters */
/* now display them six by six with a small delay */
lc.setRow(0,0,six[0]);
lc.setRow(0,1,six[1]);
lc.setRow(0,2,six[2]);
lc.setRow(0,3,six[3]);
lc.setRow(0,4,six[4]);
lc.setRow(0,5,six[5]);
lc.setRow(0,6,six[6]);
lc.setRow(0,7,six[7]);
delay(delaytime);
}
void right6() {
/* here is the data for the characters */
/* now display them six by six with a small delay */
lc.setRow(0,0,right[0]);
lc.setRow(0,1,right[1]);
lc.setRow(0,2,right[2]);
lc.setRow(0,3,right[3]);
lc.setRow(0,4,right[4]);
lc.setRow(0,5,right[5]);
lc.setRow(0,6,right[6]);
lc.setRow(0,7,right[7]);
delay(delaytime);
}
void setLight(boolean two){
if(two){
// if two is true, turn on ledPin2 and turn off ledPin
digitalWrite(ledPin2, HIGH), digitalWrite(ledPin, LOW);
} else {
// if two is false, turn off ledPin2 and turn on ledPin
digitalWrite(ledPin2, LOW), digitalWrite(ledPin, HIGH);
}
}
void loop() {
// WiFiClient client;
// HTTPClient httpClient;
//
// httpClient.begin(client, "http://koffiepunthva.nl/api");
// int httpCode = httpClient.GET();
//
// if(httpCode == HTTP_CODE_OK) { // HTTP_CODE_OK == 200
// String payload = httpClient.getString();
// Serial.println(payload);
// } else {
// Serial.println("Unable to connect :(");
// }
//
// delay(5000);
// read the state of the pushbutton value:
buttonState = digitalRead(button);
int x;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if(buttonState == HIGH)
{
// generates a random number from 1 to 6
x = random(1,7);
Serial.print("value ");
Serial.print(x);
Serial.print("\n");
switch(x){
// when x = number 1, display one1 and left1 and turn off ledPin2 and turn on ledPin
case 1 : one1(),left1(), setLight(false);
break;
// when x = number 2, display two2 and shot2 and turn on ledPin2 and turn off ledPin
case 2 : two2(),shot2(), setLight(true);
break;
// when x = number 3, display three3 and turn off ledPin2 and turn on ledPin
case 3 : three3(), setLight(false);
break;
// when x = number 4, display four4 and turn off ledPin2 and turn on ledPin
case 4 : four4(), setLight(false);
break;
// when x = number 5, display five5 and turn off ledPin2 and turn on ledPin
case 5 : five5(), setLight(false);
break;
// when x = number 6, display six6 and right6 and turn off ledPin2 and turn on ledPin
case 6 : six6(), right6(), setLight(false);
break;}
}
//{
// val=analogRead(ldrPin); // read the analog value of the sensor and assign it to val
// Serial.println(val); // display the value of val
// analogWrite(ledPin2,val);// turn on the LED and set up brightness(maximum output value 255)
// delay(10);// wait for 0.01
//}
}
update I managed to connect my wemos to my database using wifi. My code does not work with WiFiManager yet, so that’s not included in the code. My ldr sensor also doesn’t work: it is able to print the value to the serial monitor, but the value will not go higher than 6.
// Libraries
// Wifi library
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
// Led matrix
#include "LedControl.h"
const int ldrPin = A0;
const int ledPin = D6;
const int ledPin2 = D0;
int val = 0;
const int button = D2;
int buttonState = 0;
// pin D7 is connected to the DataOut
// pin D5 is connected to the CLK
// pin D8 is connected to CS
LedControl lc=LedControl(D7,D5,D8,1);
// we wait a bit between updates of the display
unsigned long delaytime=500;
byte right[8]={B01000010,B01100110,B00111100,B10011001,B11000011,B01100110,B00111100,B00011000};
byte six[8]={B00000000,B11011011,B11011011,B00000000,B00000000,B11011011,B11011011,B00000000};
byte five[8]={B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
byte four[8]={B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
byte three[8]={B11000000,B11000000,B00000000,B00011000,B00011000,B00000000,B00000011,B00000011};
byte shot[8]={B00000000,B11111111,B10100111,B10100011,B10100011,B10100111,B11111111,B00000000};
byte two[8]={B00000000,B00000000,B00000000,B01100110,B01100110,B00000000,B00000000,B00000000};
byte left[8]={B00011000,B00111100,B01100110,B11000011,B10011001,B00111100,B01100110,B01000010};
byte one[8]={B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};
void setup() {
Serial.begin(115200); //Initialize serial communication
WiFi.begin("iotroam", "7zjpji9PJn"); //SSID and password
// Keep in while-loop while the device is not connected to your accesspoint.
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Waiting on connection...
}
// Set input and outputs
pinMode(button, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
randomSeed(analogRead(0));
// The MAX72XX is in power-saving mode on startup,
// we have to do a wakeup call
lc.shutdown(0,false);
// Set the brightness to a medium values
lc.setIntensity(0,5);
// and clear the display
lc.clearDisplay(0);
}
// This method will display the characters
void one1() {
// here is the data for the characters
// now display them one by one with a small delay
lc.setRow(0,0,one[0]);
lc.setRow(0,1,one[1]);
lc.setRow(0,2,one[2]);
lc.setRow(0,3,one[3]);
lc.setRow(0,4,one[4]);
lc.setRow(0,5,one[5]);
lc.setRow(0,6,one[6]);
lc.setRow(0,7,one[7]);
delay(delaytime);
}
void left1() {
// here is the data for the characters
// now display them one by one with a small delay
lc.setRow(0,0,left[0]);
lc.setRow(0,1,left[1]);
lc.setRow(0,2,left[2]);
lc.setRow(0,3,left[3]);
lc.setRow(0,4,left[4]);
lc.setRow(0,5,left[5]);
lc.setRow(0,6,left[6]);
lc.setRow(0,7,left[7]);
delay(delaytime);
}
void two2() {
// here is the data for the characters
// now display them one by one with a small delay */
lc.setRow(0,0,two[0]);
lc.setRow(0,1,two[1]);
lc.setRow(0,2,two[2]);
lc.setRow(0,3,two[3]);
lc.setRow(0,4,two[4]);
lc.setRow(0,5,two[5]);
lc.setRow(0,6,two[6]);
lc.setRow(0,7,two[7]);
delay(delaytime);
}
void shot2() {
lc.setRow(0,0,shot[0]);
lc.setRow(0,1,shot[1]);
lc.setRow(0,2,shot[2]);
lc.setRow(0,3,shot[3]);
lc.setRow(0,4,shot[4]);
lc.setRow(0,5,shot[5]);
lc.setRow(0,6,shot[6]);
lc.setRow(0,7,shot[7]);
delay(delaytime);
}
void three3() {
// here is the data for the characters
// now display them one by one with a small delay
lc.setRow(0,0,three[0]);
lc.setRow(0,1,three[1]);
lc.setRow(0,2,three[2]);
lc.setRow(0,3,three[3]);
lc.setRow(0,4,three[4]);
lc.setRow(0,5,three[5]);
lc.setRow(0,6,three[6]);
lc.setRow(0,7,three[7]);
delay(delaytime);
}
void four4() {
// here is the data for the characters
// now display them four by four with a small delay
lc.setRow(0,0,four[0]);
lc.setRow(0,1,four[1]);
lc.setRow(0,2,four[2]);
lc.setRow(0,3,four[3]);
lc.setRow(0,4,four[4]);
lc.setRow(0,5,four[5]);
lc.setRow(0,6,four[6]);
lc.setRow(0,7,four[7]);
delay(delaytime);
}
void five5() {
// here is the data for the characters
// now display them five by five with a small delay
lc.setRow(0,0,five[0]);
lc.setRow(0,1,five[1]);
lc.setRow(0,2,five[2]);
lc.setRow(0,3,five[3]);
lc.setRow(0,4,five[4]);
lc.setRow(0,5,five[5]);
lc.setRow(0,6,five[6]);
lc.setRow(0,7,five[7]);
delay(delaytime);
}
void six6() {
// here is the data for the characters
// now display them six by six with a small delay
lc.setRow(0,0,six[0]);
lc.setRow(0,1,six[1]);
lc.setRow(0,2,six[2]);
lc.setRow(0,3,six[3]);
lc.setRow(0,4,six[4]);
lc.setRow(0,5,six[5]);
lc.setRow(0,6,six[6]);
lc.setRow(0,7,six[7]);
delay(delaytime);
}
void right6() {
// here is the data for the characters
// now display them six by six with a small delay
lc.setRow(0,0,right[0]);
lc.setRow(0,1,right[1]);
lc.setRow(0,2,right[2]);
lc.setRow(0,3,right[3]);
lc.setRow(0,4,right[4]);
lc.setRow(0,5,right[5]);
lc.setRow(0,6,right[6]);
lc.setRow(0,7,right[7]);
delay(delaytime);
}
void setLight(boolean two){
if(two){
// if two is true, turn on ledPin2 and turn off ledPin
digitalWrite(ledPin2, HIGH), digitalWrite(ledPin, LOW);
// val=analogRead(ldrPin); // read the analog value of the sensor and assign it to val
// Serial.println(val); // display the value of val
// analogWrite(ledPin2,val);// turn on the LED and set up brightness(maximum output value 255)
delay(10);// wait for 0.01
} else {
// if two is false, turn off ledPin2 and turn on ledPin
digitalWrite(ledPin2, LOW), digitalWrite(ledPin, HIGH);
}
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(button);
int x;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if(buttonState == HIGH) {
// generates a random number from 1 to 6
x = random(1,7);
Serial.print("value ");
Serial.print(x);
Serial.print("\n");
switch(x){
// when x = number 1, display one1 and left1 and turn off ledPin2 and turn on ledPin
case 1 : one1(),left1(), setLight(false);
break;
// when x = number 2, display two2 and shot2 and turn on ledPin2 and turn off ledPin
case 2 : two2(),shot2(), setLight(true);
break;
// when x = number 3, display three3 and turn off ledPin2 and turn on ledPin
case 3 : three3(), setLight(false);
break;
// when x = number 4, display four4 and turn off ledPin2 and turn on ledPin
case 4 : four4(), setLight(false);
break;
// when x = number 5, display five5 and turn off ledPin2 and turn on ledPin
case 5 : five5(), setLight(false);
break;
// when x = number 6, display six6 and right6 and turn off ledPin2 and turn on ledPin
case 6 : six6(), right6(), setLight(false);
break;}
WiFiClient client;
HTTPClient httpClient;
String url = String("http://192.168.2.7/insert.php?dice=") + x;
Serial.println(url);
if (httpClient.begin(client, url)) {
//start connection and send HTTP header
int httpCode = httpClient.GET();
if(httpCode == HTTP_CODE_OK) { // HTTP_CODE_OK == 200
String payload = httpClient.getString();
Serial.println(payload);
} else {
Serial.println("Unable to connect :(");
}
delay(500);
}
}
}
update I have the wemos connected with wifi through the http tunnel instead of the IP address from my laptop. This part of the code now looks like this:
WiFiClient client;
HTTPClient httpClient;
// String url = String("http://192.168.2.7/insert.php?dice=") + x;
String url = String("http://2fcfed7ce808e2.lhr.life/insert.php?dice=") + x;
Serial.println(url);
if (httpClient.begin(client, url)) {
//start connection and send HTTP header
int httpCode = httpClient.GET();
if(httpCode == HTTP_CODE_OK) { // HTTP_CODE_OK == 200
String payload = httpClient.getString();
Serial.println(payload);
} else {
Serial.println("Unable to connect :(");
}
delay(500);
}
update My ldr sensor works now. I’ve used this code:
void setLight(boolean two){
if(two){
// read the analog value of the ldr pin and assign it to val
val=analogRead(ldrPin);
// turn on LED 2 and set up brightness, turn LED 1 off
analogWrite(ledPin2,val), analogWrite(ledPin,0);
// wait for 1 sec.
delay(1000);
} else {
// read the analog value of the ldr pin and assign it to val
val=analogRead(ldrPin);
// turn on LED 1 and set up brightness, turn LED 2 off
analogWrite(ledPin,val), analogWrite(ledPin2,0);
// wait for 1 sec.
delay(1000);
}
}
Later, I realized that my ldr sensor and LEDS should actually work the other way around. With the code above, the LEDs turn brighter when the value of the ldr goes up. That is not what I wanted it to do. I wanted the LEDs to turn brighter when it’s darker. I’ve solved this issue with the following code:
void setLight(boolean two){
// if two is true (so when x = number 2), do the following
if(two){
// read the analog value of the ldr pin and assign it to val
val = analogRead(ldrPin);
// limits the ldr sensor values to between 400 to 500
val = constrain(val,400,500);
// re-maps value 400 to 255 (the maximum of the brightness) and 500 to 75
val = map(val,400,500,255,75);
Serial.println(val);
// // turn on LED 2 and set up brightness, turn LED 1 off
analogWrite(ledPin2, val), analogWrite(ledPin, 0);
// wait for .2 sec.
delay(200);
} else {
// read the analog value of the ldr pin and assign it to val
val = analogRead(ldrPin);
// limits the ldr sensor values to between 400 to 500
val = constrain(val,400,500);
// re-maps value 400 to 255 (the maximum of the brightness) and 500 to 75
val = map(val,400,500,255,75);
Serial.println(val);
// // turn on LED 1 and set up brightness, turn LED 2 off
analogWrite(ledPin, val), analogWrite(ledPin2, 0);
delay(200);
}
}
Fully working Arduino code¶
My Arduino code with all the sensors working now looks like this:
//Author: Kasinah Latumanuwy, student minor Internet of Things on the Hogeschool van Amsterdam
//License: Open source
//Goal: Create a digital dice with a Wemos device that's used to play a drinking game
//Libraries
//Wifi library
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
//Led matrix
#include "LedControl.h"
const int ldrPin = A0;
const int ledPin = D6;
const int ledPin2 = D0;
int val = 0;
const int button = D2;
int buttonState = 0;
/*
pin D7 is connected to the DataOut
pin D5 is connected to the CLK
pin D8 is connected to CS
*/
LedControl lc=LedControl(D7,D5,D8,1);
/* we wait a bit between updates of the display */
unsigned long delaytime=500;
byte right[8]={B01000010,B01100110,B00111100,B10011001,B11000011,B01100110,B00111100,B00011000};
byte six[8]={B00000000,B11011011,B11011011,B00000000,B00000000,B11011011,B11011011,B00000000};
byte five[8]={B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
byte four[8]={B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
byte three[8]={B11000000,B11000000,B00000000,B00011000,B00011000,B00000000,B00000011,B00000011};
byte shot[8]={B00000000,B11111111,B10100111,B10100011,B10100011,B10100111,B11111111,B00000000};
byte two[8]={B00000000,B00000000,B00000000,B01100110,B01100110,B00000000,B00000000,B00000000};
byte left[8]={B00011000,B00111100,B01100110,B11000011,B10011001,B00111100,B01100110,B01000010};
byte one[8]={B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};
void setup() {
Serial.begin(115200); //Initialize serial communication
//WiFi.begin("iotroam", "7zjpji9PJn"); //SSID and password
//WiFi.begin("nappielatu", "Kalejo2215!"); //SSID and password
WiFi.begin("WP-2018", "Merdeka1810"); //SSID and password
// Keep in while-loop while the device is not connected to your accesspoint.
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Waiting on connection...
}
//Set input and outputs
pinMode(button, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
randomSeed(analogRead(0));
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,5);
/* and clear the display */
lc.clearDisplay(0);
}
/*
This method will display the characters
*/
void one1() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,one[0]);
lc.setRow(0,1,one[1]);
lc.setRow(0,2,one[2]);
lc.setRow(0,3,one[3]);
lc.setRow(0,4,one[4]);
lc.setRow(0,5,one[5]);
lc.setRow(0,6,one[6]);
lc.setRow(0,7,one[7]);
delay(delaytime);
}
void left1() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,left[0]);
lc.setRow(0,1,left[1]);
lc.setRow(0,2,left[2]);
lc.setRow(0,3,left[3]);
lc.setRow(0,4,left[4]);
lc.setRow(0,5,left[5]);
lc.setRow(0,6,left[6]);
lc.setRow(0,7,left[7]);
delay(delaytime);
}
void two2() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,two[0]);
lc.setRow(0,1,two[1]);
lc.setRow(0,2,two[2]);
lc.setRow(0,3,two[3]);
lc.setRow(0,4,two[4]);
lc.setRow(0,5,two[5]);
lc.setRow(0,6,two[6]);
lc.setRow(0,7,two[7]);
delay(delaytime);
}
void shot2() {
lc.setRow(0,0,shot[0]);
lc.setRow(0,1,shot[1]);
lc.setRow(0,2,shot[2]);
lc.setRow(0,3,shot[3]);
lc.setRow(0,4,shot[4]);
lc.setRow(0,5,shot[5]);
lc.setRow(0,6,shot[6]);
lc.setRow(0,7,shot[7]);
delay(delaytime);
}
void three3() {
/* here is the data for the characters */
/* now display them one by one with a small delay */
lc.setRow(0,0,three[0]);
lc.setRow(0,1,three[1]);
lc.setRow(0,2,three[2]);
lc.setRow(0,3,three[3]);
lc.setRow(0,4,three[4]);
lc.setRow(0,5,three[5]);
lc.setRow(0,6,three[6]);
lc.setRow(0,7,three[7]);
delay(delaytime);
}
void four4() {
/* here is the data for the characters */
/* now display them four by four with a small delay */
lc.setRow(0,0,four[0]);
lc.setRow(0,1,four[1]);
lc.setRow(0,2,four[2]);
lc.setRow(0,3,four[3]);
lc.setRow(0,4,four[4]);
lc.setRow(0,5,four[5]);
lc.setRow(0,6,four[6]);
lc.setRow(0,7,four[7]);
delay(delaytime);
}
void five5() {
/* here is the data for the characters */
/* now display them five by five with a small delay */
lc.setRow(0,0,five[0]);
lc.setRow(0,1,five[1]);
lc.setRow(0,2,five[2]);
lc.setRow(0,3,five[3]);
lc.setRow(0,4,five[4]);
lc.setRow(0,5,five[5]);
lc.setRow(0,6,five[6]);
lc.setRow(0,7,five[7]);
delay(delaytime);
}
void six6() {
/* here is the data for the characters */
/* now display them six by six with a small delay */
lc.setRow(0,0,six[0]);
lc.setRow(0,1,six[1]);
lc.setRow(0,2,six[2]);
lc.setRow(0,3,six[3]);
lc.setRow(0,4,six[4]);
lc.setRow(0,5,six[5]);
lc.setRow(0,6,six[6]);
lc.setRow(0,7,six[7]);
delay(delaytime);
}
void right6() {
/* here is the data for the characters */
/* now display them six by six with a small delay */
lc.setRow(0,0,right[0]);
lc.setRow(0,1,right[1]);
lc.setRow(0,2,right[2]);
lc.setRow(0,3,right[3]);
lc.setRow(0,4,right[4]);
lc.setRow(0,5,right[5]);
lc.setRow(0,6,right[6]);
lc.setRow(0,7,right[7]);
delay(delaytime);
}
void setLight(boolean two){
// if two is true (so when x = number 2), do the following
if(two){
// read the analog value of the ldr pin and assign it to val
val = analogRead(ldrPin);
// limits the ldr sensor values to between 400 to 500
val = constrain(val,400,500);
// re-maps value 400 to 255 (the maximum of the brightness) and 500 to 75
val = map(val,400,500,255,75);
Serial.println(val);
// // turn on LED 2 and set up brightness, turn LED 1 off
analogWrite(ledPin2, val), analogWrite(ledPin, 0);
// wait for .2 sec.
delay(200);
} else {
// read the analog value of the ldr pin and assign it to val
val = analogRead(ldrPin);
// limits the ldr sensor values to between 400 to 500
val = constrain(val,400,500);
// re-maps value 400 to 255 (the maximum of the brightness) and 500 to 75
val = map(val,400,500,255,75);
Serial.println(val);
// // turn on LED 1 and set up brightness, turn LED 2 off
analogWrite(ledPin, val), analogWrite(ledPin2, 0);
delay(200);
}
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(button);
int x;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if(buttonState == HIGH) {
// generates a random number from 1 to 6
x = random(1,7);
Serial.print("value ");
Serial.print(x);
Serial.print("\n");
switch(x){
// when x = number 1, display one1 and left1 and turn off ledPin2 and turn on ledPin
case 1 : one1(),left1(), setLight(false);
break;
// when x = number 2, display two2 and shot2 and turn on ledPin2 and turn off ledPin
case 2 : two2(),shot2(), setLight(true);
break;
// when x = number 3, display three3 and turn off ledPin2 and turn on ledPin
case 3 : three3(), setLight(false);
break;
// when x = number 4, display four4 and turn off ledPin2 and turn on ledPin
case 4 : four4(), setLight(false);
break;
// when x = number 5, display five5 and turn off ledPin2 and turn on ledPin
case 5 : five5(), setLight(false);
break;
// when x = number 6, display six6 and right6 and turn off ledPin2 and turn on ledPin
case 6 : six6(), right6(), setLight(false);
break;}
WiFiClient client;
HTTPClient httpClient;
// String url = String("http://192.168.2.7/insert.php?dice=") + x;
String url = String("http://866e85efa4d87f.lhr.life/insert.php?dice=") + x;
Serial.println(url);
if (httpClient.begin(client, url)) {
//start connection and send HTTP header
int httpCode = httpClient.GET();
if(httpCode == HTTP_CODE_OK) { // HTTP_CODE_OK == 200
String payload = httpClient.getString();
Serial.println(payload);
} else {
Serial.println("Unable to connect :(");
}
delay(500);
}
}
}
Links¶
- https://diyi0t.com/segment-led-display-tutorial-for-arduino-and-esp8266/
- https://github.com/viratbadri/Electro-oxe/blob/master/codes/dice_max.ino
- https://www.youtube.com/watch?v=bkrELCsit1Q
- https://www.instructables.com/Automatic-Lights-Using-LDR-Brightness-Control/