0

For assignment 7 I'm trying to make a timer. Im trying to get the count down in an easier code. Now I have a whole list of: display.draw2x2String (4, 2, "08"); going from 10 to 0

I was wondering if it is also possible to use a for loop, something like this;

for(int i = 10; i > 0; i--){ display.draw2x2String (4, 2, i); delay (1000); However, I am not able to get it displayed properly. What can I do to fix this?

Share a link to this question (includes your user id)
| edit | | close | delete |
0

Since the loop is already repeating, I would do something like this:

#include <U8g2lib.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C display(U8X8_PIN_NONE);

int count = 10;

char buf[10]; // text buffer; to be able to use draw2x2String to show the value

void setup() {
  display.begin();
  display.setPowerSave(0);
  display.setFont(u8x8_font_pxplusibmcgathin_f);
}

void loop() {
  if (count>=0) { // if counter not finished
    snprintf(buf, 10, "%2d", count);
    display.draw2x2String(4,3,buf);
    count = count - 1; // decrease counter
  }
  delay(1000); // wait one second
}

Hope this helps!

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.