I'm trying to display a random image and display it on a lcd. The code is modified from a sketch which cycled through a few images, however I would like the first image to be followed by a random image from a selection of 4.
I have tried to combine two sketches, but the random image doesn't work.
Any guidance would be appreciated.
/**
*/
#include <Nokia_LCD.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
const uint8_t amount_of_images = 4;
const char* const image [amount_of_images] PROGMEM={
"image1",
"image2",
"image3",
"image4"
};
const unsigned char image0 [] PROGMEM = {
};
const unsigned char image1 [] PROGMEM = {
};
const unsigned char image2 [] PROGMEM = {
};
const unsigned char image3 [] PROGMEM = {
};
const unsigned char image4 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0xF0, 0xE8, 0xF0, 0xB0, 0x38, 0x28, 0x20, 0x20, 0x20, 0x20,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xC0, 0x70, 0x18, 0x0C, 0x06, 0x02, 0x02, 0x06, 0x1C, 0x30, 0x20, 0x60, 0xC0, 0xC0, 0x40,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0xC0, 0xE0, 0x00, 0x80, 0xC0, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x1F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x3C, 0x94, 0xF2, 0x3B, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81,
0x00, 0x01, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x1A, 0x0A, 0x0E, 0x7E, 0x0F, 0x01, 0x03, 0x0F, 0x1A,
0x32, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0xE0, 0x78, 0x2E, 0x23, 0xA1, 0xE0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0D, 0x19, 0x31, 0xE1, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xC0, 0xC0, 0xF8, 0xC0, 0x60, 0x78, 0x40, 0x40, 0x40,
0x00, 0x00, 0x00, 0x00, 0x80, 0xE4, 0x30, 0x98, 0x86, 0x83, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x03, 0x03, 0xC3, 0x47, 0x4D,
0x79, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x1C, 0x06, 0x7F, 0x03, 0x06,
0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x63, 0x71, 0x39, 0x2E, 0x33, 0x10, 0x12,
0x10, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0xF8, 0x0C, 0xF4, 0x04, 0x04, 0x08, 0x08, 0x1A, 0x10,
0x10, 0x31, 0x23, 0x6C, 0x78, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0xC0, 0xF8, 0xC0, 0xF0, 0x90, 0x80,
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x1D, 0x32, 0x63, 0x42, 0x43, 0x62, 0x33,
0x19, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x31, 0x19, 0x0D, 0x3F,
0x01, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
};
Nokia_LCD lcd(PB4 /* CLK */, PB3 /* DIN */, PB2 /* DC */, PB1 /* CE */, PB0 /* RST */);
volatile bool watchdogBarked = false;
enum WatchDogTimeout {
WDT_16ms = 0,
WDT_32ms,
WDT_64ms,
WDT_128ms,
WDT_250ms,
WDT_500ms,
WDT_1sec,
WDT_2sec,
WDT_4sec,
WDT_8sec
};
/**
Watchdog interrupt routine to be triggered when watchdog times out.
*/
ISR(WDT_vect) {
watchdogBarked = true;
}
void goToSleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
power_all_disable ();
sleep_enable();
sleep_cpu(); // Sleep here and wait for the interrupt
sleep_disable();
power_all_enable(); // power everything back on
}
/**
Sets up watchdog to be triggered (once) after the specified time
@param wdt the watchdog timeout duration
*/
void triggerWatchDogIn(WatchDogTimeout wdt) {
// Adopted from InsideGadgets (www.insidegadgets.com)
byte timeoutVal = wdt & 7;
if (wdt > 7) {
timeoutVal |= (1 << 5);
}
timeoutVal |= (1 << WDCE);
MCUSR &= ~(1 << WDRF);
WDTCR |= (1 << WDCE) | (1 << WDE); // Start timed sequence
WDTCR = timeoutVal;
WDTCR |= _BV(WDIE);
wdt_reset(); // Pat the dog
}
/**
A utility method to derive a watchdog timeout's duration
@param wdt the watchdog timeout
@return the amount of milliseconds corresponding to a watchdog timeout
*/
unsigned long getTimeoutDuration(WatchDogTimeout wdt) {
return 1 << (wdt + 4);
}
/**
Blocks and stays in deep sleep until the specified time has elapsed
using the current watchdog timeout.
@param sleepDuration how long to stay in deep sleep in milliseconds
@param timeoutInterval the watchdog timeout interval
*/
void stayInDeepSleepFor(unsigned long sleepDuration, WatchDogTimeout timeoutInterval = WDT_16ms) {
unsigned long sleepTime = 0;
// Start by triggering the watchdog to wake us up every `timeoutInterval`
triggerWatchDogIn(timeoutInterval);
while (sleepTime <= sleepDuration) {
// Sleep until an interrupt occurs (external, change or watchdog)
goToSleep();
// Verify we woke up because of the watchdog and not
// a spurious wake up due to some other unrelated interrupt.
if (watchdogBarked) {
// Note down that we have processed the watchdog bark
watchdogBarked = false;
// Increase the time we have already slept
sleepTime += getTimeoutDuration(timeoutInterval);
}
}
wdt_disable(); // Disable watchdog so it stops barking
}
void setup() {
// Initialize the screen
lcd.begin();
lcd.setCursor(0, 0);
}
void loop() {
// put your main code here, to run repeatedly:
// Draw the intro pic on your screen
lcd.draw(image0, 504);
stayInDeepSleepFor(2000);
// Draw the random image on your screen
lcd.draw(random (image), 504);
stayInDeepSleepFor(10000);
}