I am a beginner at Arduino and am not the best at the code, but I want to create a 2:00 countdown timer with a .96 I2C OLED display and I can't find anyone else who did it, so can you help me >:(
Do you have a library for the oled and have an example program for that working?
2:00 hours? Minutes? What happens at 0:00?
This assumes a 128x64 OLED. Compiles and works in the serial monitor; not tested with an OLED.
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
#define COUNTDOWN_TIME 121000ul
char
szString[20];
byte
mins, secs;
unsigned long
timeTemp,
timeNow,
timeStart,
timeElapsed,
timeLeft;
Adafruit_SSD1306 display(-1);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup()
{
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
Serial.begin(9600);
timeStart = millis();
mins = 1;
secs = 1;
}//setup
void DoCountdown()
{
static unsigned long
lastTimeNow = 0;
static byte
lastsecs = 1;
timeNow = millis();
timeElapsed = timeNow - timeStart;
if( mins == 0 && secs == 0 )
return;
timeLeft = COUNTDOWN_TIME - timeElapsed;
mins = (byte)(timeLeft / 60000ul);
timeTemp = timeLeft - (mins * 60000);
secs = (byte)(timeTemp / 1000ul);
timeTemp = timeTemp - (secs * 1000ul);
if( mins == 0 && secs == 0 )
{
sprintf( szString, "**DONE**" );
Serial.println( szString );
display.setCursor( 44, 30 );
display.print( szString );
display.display();
}//if
else if( secs != lastsecs )
{
lastsecs = secs;
sprintf( szString, "%02d:%02d", mins, secs );
Serial.println( szString );
//
display.setCursor( 50, 30 );
display.print( szString );
display.display();
}//if
}//DoCountdown
void loop()
{
DoCountdown();
}//loop
(deleted)
spycatcher2k:
@BlackfinIf you're going to write sketches for any Tom, Dick or Harry that asks for one, you really need to start adding comments explaining what is going on.
Perhaps he or she belongs to the "it was hard to write, therefore it should be hard to read" school.
I’ll go Spycatcher2k one better, I’d ask Bluefin to please stop the practice, at least on the first request - as it completely shuts down any chance of the OP learning anything. It’s one thing when they’ve tried and they’re stuck on a concept or just cannot see a solution. But to hand someone what’s asked for on the first request with a less than clear set of requirements really helps no one, least of all the OP.
He’s free to do whatever he wants but I think it’s counterproductive to be doing many posters homework assignments for them.
WattsThat:
I’ll go Spycatcher2k one better, I’d ask Bluefin to please stop the practice, at least on the first request - as it completely shuts down any chance of the OP learning anything.
One of the problems I've oft cited here is that people claim this is a site for learning when in fact that's not the case. In fact it's more often a platform for old timers here to berate, belittle, condescend and bully those that don't already know it all. This is a strange property of this forum I can't recall seeing on any other forum dedicated to what is ostensibly a hobby/educational platform.
That is far worse than posting a simple (hopefully) functional example of what they're asking that they can take offline and study, modify and customize at their leisure. It's only a bit further along the line than berating them to study BWOD and cutting them loose with a snide comment.
He’s free to do whatever he wants but I think it’s counterproductive to be doing many posters homework assignments for them.
If an OP doesn't know the basics in his class and, out of the blue, submits functional, formatted code that might have a few syntactical concepts obviously beyond his level of programming and what's done prior to that I'm sure his/her educator will clue in. It's not at all unusual for educators to search forums and the web for hints about how a 'C' student suddenly started submitting got 'A'-level code (if I do say so myself...)
Besides, not everyone is doing homework. Some people just want a piece of code to get a home automation project finished and done with. Maybe he's doing something for his kids. I feel no qualms about posting reasonably functional code for those folks.
It's not a responder's duty to ponder requestors' motives in asking for help: that includes not being their conscience when it comes to homework, nor insisting that anyone learns anything. (Although as a tertiary grad myself, and father of two such, it pains me to think that those asking for help here may be passing off forum work as their own.) But let's not dilute this OP's thread too much with that debate.
I am though, in total agreement with spycatcher2k (I'd love to know the story behind that nick) when she says Blackfin (or as WattsThat calls him or her, Bluefin) should bung some comments in the code.
That will make this:
they can take offline and study, modify and customize at their leisure
....much easier.