Skip to content

Instantly share code, notes, and snippets.

@xxlukas42
Last active December 24, 2019 11:13
Show Gist options
  • Save xxlukas42/7939909cc6f9c3d199facec837620fca to your computer and use it in GitHub Desktop.
Save xxlukas42/7939909cc6f9c3d199facec837620fca to your computer and use it in GitHub Desktop.
Wemos D1 Shields examples
/*
Wemos D1 mini/D1 lite
Buzzer Shield example
by Petr Lukas
Simple music, sound efects and cricket chirp sounds
*/
int buzzer=D5; //Buzzer control port, default D5
void setup() {
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
Serial.begin(115200);
Serial.println("Buzzer Test...");
musicTest();
delay(1000);
ohhh();
delay(1000);
uhoh();
delay(1000);
cricketChirp();
cricketChirp();
cricketChirp();
cricketChirp();
}
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration, unsigned long pause) {
pinMode (_pin, OUTPUT );
analogWriteFreq(frequency);
analogWrite(_pin,500);
delay(duration);
analogWrite(_pin,0);
delay(pause);
}
void beep (int speakerPin, float noteFrequency, long noteDuration){
int x;
// Convert the frequency to microseconds
float microsecondsPerWave = 1000000/noteFrequency;
// Calculate how many milliseconds there are per HIGH/LOW cycles.
float millisecondsPerCycle = 1000/(microsecondsPerWave * 2);
// Multiply noteDuration * number or cycles per millisecond
float loopTime = noteDuration * millisecondsPerCycle;
// Play the note for the calculated loopTime.
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(microsecondsPerWave);
digitalWrite(speakerPin,LOW);
delayMicroseconds(microsecondsPerWave);
}
}
void musicTest(){
delay(1000);
tone(buzzer, 400, 600, 150);
tone(buzzer, 400, 600, 150);
tone(buzzer, 400, 600, 150);
tone(buzzer, 320, 400, 80);
tone(buzzer, 475, 150, 80);
tone(buzzer, 400, 600, 100);
tone(buzzer, 320, 400, 80);
tone(buzzer, 475, 150, 80);
tone(buzzer, 400, 600, 100);
}
void ohhh() {
for (int i=1000; i<2000; i=i*1.02) { beep(buzzer,i,10); } for (int i=2000; i>1000; i=i*.98) {
beep(buzzer,i,10);
}
}
void uhoh() {
for (int i=1000; i<1244; i=i*1.01) { beep(buzzer,i,30); } delay(200); for (int i=1244; i>1108; i=i*.99) {
beep(buzzer,i,30);
}
}
void cricketChirp(){
for(int i=0; i<4; i++){
tone(buzzer, 4000, 20, 15);
}
delay(450);
}
void loop() {
}
/*
Wemos D1 mini/D1 lite
Matrix LED Shield example
by Marek Mravik & Petr Lukas
*/
#include <WEMOS_Matrix_LED.h>
MLED mled(0); //set intensity=0
//change this to increase maximum size of the string
#define MAX_SIZE 1024
//make this global:
int left = 0;
int right = 8;
// List of all printable ASCII chars
const char ALL_CHARACTERS[100][8] = {
{0,0,0,0,0,0,0,0},// space
{0,0,48,125,125,48,0,0},//!
{0,0,96,112,0,112,96,0},//"
{0,20,127,127,20,127,127,20},//#
{0,0,18,42,107,42,36,0},//$
{0,98,102,12,24,51,35,0},//%
{0,38,127,81,89,119,35,5},//&
{0,0,0,0,96,112,16,0},//'
{0,0,0,28,62,99,65,0},//(
{0,65,99,62,28,0,0,0},//)
{0,8,42,62,28,62,42,8},//*
{0,0,8,8,62,8,8,0},//+
{0,1,15,14,0,0,0,0},//,
{0,0,8,8,8,8,0,0},//-
{0,3,3,0,0,0,0,0},//.
{0,2,6,12,24,48,32,0},///
{0,62,127,73,81,127,62,0},//0
{0,1,17,127,127,1,1,0},//1
{0,35,103,69,73,121,49,0},//2
{0,34,99,73,73,127,54,0},//3
{0,12,20,36,127,127,4,0},//4
{0,114,115,81,81,95,78,0},//5
{0,62,127,73,73,111,38,0},//6
{0,96,96,71,95,120,96,0},//7
{0,54,127,73,73,127,54,0},//8
{0,50,123,73,73,127,62,0},//9
{0,0,0,54,54,0,0,0},//:
{0,0,1,55,54,0,0,0},//;
{0,0,8,28,54,99,65,0},//<
{0,0,20,20,20,20,0,0},//=
{0,65,99,54,28,8,0,0},//>
{0,32,96,77,77,120,48,0},//?
{0,60,66,90,90,50,4,0},//@
// Letters A-Z
{0,63,127,72,72,127,63,0},
{0,127,127,73,73,127,54,0},
{0,62,127,65,65,99,34,0},
{0,127,127,65,65,127,62,0},
{0,127,127,73,73,73,65,0},
{0,127,127,72,72,72,64,0},
{0,62,127,65,69,103,38,0},
{0,127,127,8,8,127,127,0},
{0,0,65,127,127,65,0,0},
{0,6,7,65,127,126,64,0},
{0,127,127,28,54,99,65,0},
{0,127,127,1,1,1,1,0},
{0,127,127,48,24,48,127,127},
{0,127,127,48,24,12,127,127},
{0,62,127,65,65,127,62,0},
{0,127,127,68,68,124,56,0},
{0,60,126,66,70,127,61,0},
{0,127,127,76,78,123,49,0},
{0,50,123,73,73,111,38,0},
{0,96,64,127,127,64,96,0},
{0,126,127,1,1,127,127,0},
{0,124,126,3,3,126,124,0},
{0,127,127,6,12,6,127,127},
{0,99,119,28,8,28,119,99},
{0,112,120,15,15,120,112,0},
{0,67,71,77,89,113,97,0},
{0,0,0,127,127,65,65,0},//[
{0,32,48,24,12,6,2,0}, /* \ */
{0,65,65,127,127,0,0,0},//]
{0,8,16,32,64,32,16,8},//^
{0,0,1,1,1,1,0,0},//_
{0,0,0,0,96,112,16,0},//`
// Letters a-z
{0,2,23,21,21,31,15,0},
{0,127,127,9,9,15,6,0},
{0,14,31,17,17,27,10,0},
{0,6,15,9,9,127,127,0},
{0,14,31,21,21,29,12,0},
{0,4,63,127,68,100,32,0},
{0,24,61,37,37,63,62,0},
{0,127,127,8,8,15,7,0},
{0,0,1,47,47,1,0,0},
{0,6,7,1,95,94,0,0},
{0,127,127,4,14,27,17,0},
{0,0,0,127,127,0,0,0},
{0,31,31,12,7,12,31,31},
{0,31,31,24,24,31,15,0},
{0,14,31,17,17,31,14,0},
{0,63,63,36,36,60,24,0},
{0,24,60,36,63,63,1,3},
{0,31,31,16,16,28,12,0},
{0,9,21,21,21,21,18,0},
{0,8,8,63,63,8,8,0},
{0,30,31,1,1,31,31,0},
{0,12,14,3,3,14,12,0},
{0,30,31,1,15,1,31,30},
{0,17,27,14,14,27,17,0},
{0,24,29,5,5,31,30,0},
{0,0,19,23,29,25,0,0},
{0,0,8,62,119,65,65,0},//{
{0,0,0,255,255,0,0,0},//|
{0,65,65,119,62,8,0,0},//}
{0,8,24,16,24,8,16,0},//~
};
/*
simply replaces every character in string by
its representation taken from list above
*/
int converter(const char *str, char *whereTo) {
int counter = 0;
int actualIndex = 0;
while (strlen(&str[actualIndex]) > 0 && counter < MAX_SIZE) {
int shift = 0;
if ((int)str[actualIndex] > 32) {
shift = (int)str[actualIndex] - 32;
}
int i;
for (i = 0; i < 8; i++) {
whereTo[counter] = (int)ALL_CHARACTERS[shift][i];
counter++;
}
actualIndex++;
}
memset(whereTo + counter, (char)0, MAX_SIZE - counter);
return counter;
}
/*
removes spaces where they are not necessary
00011000 01100000 01101100000
00011000 01100000 01101100000
00011000 01100000 01101100000
00011000 01100000 --- \ 01101100000
00011000 01100000 --- / 01101100000
00011000 01100000 01101100000
00011000 01100000 01101100000
00011000 01111111 01101111111
*/
int removeSpaces(const char *str, int sizeOfText, char *n) {
int atOld = 0;
int atNew = 0;
int counter = 0;
char spaces[3];
spaces[0] = (char)0;
spaces[1] = (char)0;
spaces[2] = (char)0;
while (atOld < sizeOfText - 1) {
counter = 0;
while ((int)str[atOld + counter] == 0) {
counter++;
}
if (counter > 7) {
strncpy(n + atNew, spaces, 3);
atNew += 3;
atOld += counter;
} else {
atOld += counter;
counter = 0;
while ((int)str[atOld + counter] != 0 && counter < 8) {
counter++;
}
strncpy(n + atNew, str + atOld, counter);
atNew += counter;
//printf("at %i : %i\n", atNew, (int)spaces[0]);
strncpy (n + atNew, spaces, 1);
atNew++;
atOld += counter;
}
}
strncpy(n + atNew, spaces, 3);
atNew += 3;
strncpy(n + atNew, spaces, 3);
atNew += 3;
return atNew;
}
/*
according to our left and right global variables
puts respective columns that should be shown to toShow
*/
void getNextScreen(char *toShow, char* text, int sizeOfText) {
if (left < right && right < sizeOfText) {
for (int i = 0; i < 8; i++) {
toShow[i] = text[left + i];
}
} else {
int i = 0;
for (; i < sizeOfText - left; i++) {
toShow[i] = text[left + i];
}
int j = 0;
while (i < 8) {
toShow[i] = text[j];
i++;
j++;
}
}
}
/*
ligths up proper leds according to actual screen
*/
void printer(const char *toShow) {
mled.clear();
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if (toShow[j] & (1 << i)) {
mled.dot(j, i);
}
}
}
mled.display();
}
/*
calling previous functions in order and displaying on screen
*/
void stringToScreen(const char* string) {
char converted[MAX_SIZE];
char withoutSpaces[MAX_SIZE];
char nextScreen[8];
int convertedSize = converter(string, converted);
int sizeWithoutSpaces = removeSpaces(converted, convertedSize, withoutSpaces);
getNextScreen(nextScreen, withoutSpaces, sizeWithoutSpaces);
printer(nextScreen);
//we have to change our left right parameter, otherwise the text wouldn't roll
left = (left + 1 < sizeWithoutSpaces) ? left + 1 : 0;
right = (right + 1 < sizeWithoutSpaces) ? right + 1 : 0;
}
void setup() {
Serial.begin(9600);
}
void loop() {
stringToScreen(/*put your string here*/"Hello World! 1234567890 !@#$%^&*()+-*/");
delay(300);
}
/*
Wemos D1 mini/D1 lite
OLED Shield example (based on example of Jim Lindblom @ SparkFun Electronics)
by Petr Lukas
*/
#include <SPI.h>
#include <Wire.h>
#include <SFE_MicroOLED.h>
// OLED shield settings
#define PIN_RESET 255 //
#define DC_JUMPER 0 // I2C Addres: 0 - 0x3C, 1 - 0x3D
MicroOLED oled(PIN_RESET, DC_JUMPER); // Example I2C declaration
int SCREEN_WIDTH = oled.getLCDWidth();
int SCREEN_HEIGHT = oled.getLCDHeight();
float d = 3;
float px[] = {
-d, d, d, -d, -d, d, d, -d };
float py[] = {
-d, -d, d, d, -d, -d, d, d };
float pz[] = {
-d, -d, -d, -d, d, d, d, d };
float p2x[] = {
0,0,0,0,0,0,0,0};
float p2y[] = {
0,0,0,0,0,0,0,0};
float r[] = {
0,0,0};
#define SHAPE_SIZE 600
// Define how fast the cube rotates. Smaller numbers are faster.
// This is the number of ms between draws.
#define ROTATION_SPEED 0
void setup()
{
oled.begin();
oled.clear(PAGE);
oled.setCursor(0, 0);
oled.print("Test of Wemos OLED shield");
oled.display();
delay(2000);
oled.clear(PAGE);
oled.setFontType(1);
oled.setCursor(0, 0);
oled.print("1234");
oled.display();
delay(2000);
oled.clear(PAGE);
oled.setFontType(2);
oled.setCursor(0, 0);
oled.print("1234");
oled.display();
delay(2000);
oled.clear(PAGE);
oled.setFontType(3);
oled.setCursor(0, 0);
oled.print("1234");
oled.display();
delay(2000);
oled.clear(ALL);
oled.display();
}
void loop()
{
drawCube();
delay(ROTATION_SPEED);
}
void drawCube()
{
r[0]=r[0]+PI/180.0; // Add a degree
r[1]=r[1]+PI/180.0; // Add a degree
r[2]=r[2]+PI/180.0; // Add a degree
if (r[0] >= 360.0*PI/180.0) r[0] = 0;
if (r[1] >= 360.0*PI/180.0) r[1] = 0;
if (r[2] >= 360.0*PI/180.0) r[2] = 0;
for (int i=0;i<8;i++)
{
float px2 = px[i];
float py2 = cos(r[0])*py[i] - sin(r[0])*pz[i];
float pz2 = sin(r[0])*py[i] + cos(r[0])*pz[i];
float px3 = cos(r[1])*px2 + sin(r[1])*pz2;
float py3 = py2;
float pz3 = -sin(r[1])*px2 + cos(r[1])*pz2;
float ax = cos(r[2])*px3 - sin(r[2])*py3;
float ay = sin(r[2])*px3 + cos(r[2])*py3;
float az = pz3-150;
p2x[i] = SCREEN_WIDTH/2+ax*SHAPE_SIZE/az;
p2y[i] = SCREEN_HEIGHT/2+ay*SHAPE_SIZE/az;
}
oled.clear(PAGE);
for (int i=0;i<3;i++)
{
oled.line(p2x[i],p2y[i],p2x[i+1],p2y[i+1]);
oled.line(p2x[i+4],p2y[i+4],p2x[i+5],p2y[i+5]);
oled.line(p2x[i],p2y[i],p2x[i+4],p2y[i+4]);
}
oled.line(p2x[3],p2y[3],p2x[0],p2y[0]);
oled.line(p2x[7],p2y[7],p2x[4],p2y[4]);
oled.line(p2x[3],p2y[3],p2x[7],p2y[7]);
oled.display();
}
/*
Wemos D1 mini/D1 lite
SHT30 Shield example
by Petr Lukas
NOTE: Use this shield together with Dual or Tripler base to have correct temperature measurements.
You can get a inaccurate/higher temperature when using it directly above the Wemos board.
*/
#include <WEMOS_SHT3X.h>
SHT3X sht30(0x45);
void setup() {
Serial.begin(115200);
}
void loop() {
if(sht30.get()==0){
Serial.print("Temperature in Celsius : ");
Serial.println(sht30.cTemp);
Serial.print("Temperature in Fahrenheit : ");
Serial.println(sht30.fTemp);
Serial.print("Relative Humidity : ");
Serial.println(sht30.humidity);
Serial.println();
}
else
{
Serial.println("Error!");
}
delay(1000);
}
/*
Wemos D1 mini/D1 lite
WS2812B RGB Shield example
by Petr Lukas
*/
#include <Adafruit_NeoPixel.h>
#define PIN D2
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
int i,j,k;
i = random(0, 255);
//j = random(0, 255);
//k = random(0, 255);
pixels.setPixelColor(0, pixels.Color(128, 0, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(500); // Delay for a period of time (in milliseconds).
pixels.setPixelColor(0, pixels.Color(0, 128, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(500);
pixels.setPixelColor(0, pixels.Color(0, 0, 128)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment