/*
Final working version on Nano and Pro mini
Rolls 1 to three dice on OLED display
Dice roll ound
Start up screen
Sleep mode for screen and arduino
button input uses one pin resistor ladder of 2.2k, 1k, 3.3k
Memory: Membase fremem nano=1828 ; this prog 1517 used = 311
*/
#include "U8glib.h"
#include "MemoryFree.h";
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
char button = A0; // pushbutton pin
byte ndice;
int band1 = 100;
int band2 =350;
int band3 =700;
byte piezo =13; // piezo buzzer
byte fsize;
byte foffset;
byte foffset2;
byte ccent;
byte cdown;
byte coff;
byte cdot;
byte ran[4];
byte j;
byte screensleep=20; // screen sleeptime in seconds
unsigned long ntime; // time of last display
void setup() {
randomSeed(analogRead(1));
// memory check
Serial.begin(9600);
Serial.print(F("freeMemory()=")); // reports free memory
Serial.println(freeMemory());
ntime=millis();
welcome();
}
void draw2(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_6x13); // 2 lines 10 pixels
u8g.drawFrame(foffset2,16,fsize,fsize);
u8g.drawStr( 0, 30, "Welcome to Dice Roll");
u8g.drawStr(0, 60, "Press a Button");
}
void sleep1()
{
// picture loop
u8g.firstPage();
do {
u8g_SleepOn; // put screen to sleep
} while( u8g.nextPage() );
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g_SleepOff; // if a button is pressed wake up screen
u8g.setFont(u8g_font_6x13); // 2 lines 10 pixels
u8g.drawFrame(foffset2,16,fsize,fsize);
if (ran[j+1]== 1 || ran[j+1]== 3 || ran[j+1]== 5) u8g.drawDisc(ccent, cdown, cdot); // centre dot
if (ran[j+1] > 1) { u8g.drawDisc(ccent-coff, cdown+coff, cdot); u8g.drawDisc(ccent+coff, cdown-coff, cdot); } // diagonal dots for 2
if (ran[j+1] > 3) {u8g.drawDisc(ccent+coff, cdown+coff, cdot); u8g.drawDisc(ccent-coff, cdown-coff, cdot); } // diagonal dots for 4
if (ran[j+1] > 5) { u8g.drawDisc(ccent+coff, cdown, cdot); u8g.drawDisc(ccent-coff, cdown, cdot); } // diagonal dots for 6
//u8g.drawStr( 0, 20, "Hello World!");
}
void welcome()
{
// picture loop
u8g.firstPage();
do {
draw2();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(1000);
}
void loop() {
if ((millis()-ntime) > (1000*screensleep)) sleep1(); // after 20 secs put screen to sleep
//check which button is pressed
int butin = analogRead(button);
//int button = (analogRead(A0)*5*1.5+512)/1024;
if (butin < band1) ndice=1;
else if (butin < band2) ndice=2;
else if (butin < band3) ndice=3;
else ndice=0;
Serial.print(F("button value ")); // reports free memory
Serial.print(butin);
Serial.print(F("button ")); // reports free memory
//delay(1000);
Serial.println(ndice);
if (ndice > 0)
{
ntime=millis(); // and reset sleep timer
for (byte rr = 0; rr < 20; rr++) { // generate 10 dice to show spin
for (byte kk = 0; kk < ndice+1; kk++) { ran[kk] = int (random(1, 7));}
// dice roll sound??
for (byte tt = 0; tt < 1; tt++) {
tone (piezo,65,10); // orig tone (3,65,10);(3,120,10);
delay (90);
digitalWrite (piezo,LOW);
}
// picture loop
// dice generation
u8g.firstPage();
do {
fsize = 48;
if (ndice==3) fsize=44;
foffset = (127-(ndice*fsize)-ndice)/2;
for ( j = 0; j < ndice; j++)
{ // set up dice parameters
foffset2=4+(foffset+fsize)*j;
if (ndice==1) foffset2=24; //foffset2*2;
ccent=foffset2+(fsize/2);
cdown=16+(fsize/2);
coff=11; if (ndice==3) coff=10;
cdot=4; if (ndice==3) cdot=3;
draw();
}
} while( u8g.nextPage() );
// delay(30);
}
} // end of rr
} // end loop
Comments