Page 1 of 1

Arduino Counter

Posted: Mon Apr 21, 2014 6:30 am
by jsmiller12009
I'm fairly new at arduino programming and even newer at displays. I've built a sensor counter and I need the changing numbers to be displayed on the matrix display. I've played around with a few things to no avail (such as converting to strings). Below is the code and I have marked what needs to be displayed. Any suggestions or help would be greatly appreciated. If curious, the if-statement it's in says to count up if the sensor is tripped.

Code: Select all

#include <DMD.h> // for DMD
#include <SPI.h> // SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <TimerOne.h> 
#include "SystemFont5x7.h"
#include "Arial_black_16.h"
#define DISPLAYS_ACROSS 1 // change to 2 for two screens, etc. 
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

void ScanDMD() // necessary interrupt handler for refresh scanning of DMD
{ 
  dmd.scanDisplayBySPI();
}

int pirPin = 2; 
int counter = 0;
int laststate = HIGH;

void setup()
{
 Timer1.initialize( 5000 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
 Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()  
 dmd.clearScreen( true );
 Serial.begin(9600);
 pinMode(pirPin,INPUT_PULLUP);
}

void loop()
{
  int state = digitalRead(pirPin);
  if (laststate == LOW && state == HIGH) // only count on a LOW-> HIGH transition
  {
     counter++;
     Serial.println(counter);
     //DMD Font
     //Display changing counter number on DMD
  }
  laststate = state;  // remember last state
}

Re: Arduino Counter

Posted: Mon Apr 21, 2014 8:43 am
by stryker
Hi,

You could convert counter to a string representation and use drawString(), which would look something like this:

Code: Select all

    //Display changing counter number on DMD
    dmd.clearScreen( true );
    String counterAsString = String(counter);
    char stringBuffer[6];
    counterAsString.toCharArray(stringBuffer, 6);
    dmd.drawString(0,0,stringBuffer, strlen(stringBuffer), GRAPHICS_NORMAL);
or you could do what the demo clock sketch does and output a character at a time using modulo maths to work out what each place is. That would look something like this:

Code: Select all

    //Display changing counter number on DMD (up to value 9999)
    dmd.clearScreen( true );
    if(counter > 999) dmd.drawChar(1,0, '0'+((counter%10000)/1000), GRAPHICS_NORMAL);     // thousands
    if(counter > 99) dmd.drawChar(7,0, '0'+((counter%1000)/100), GRAPHICS_NORMAL);        // hundreds
    if(counter > 9) dmd.drawChar(12,0, '0'+((counter%100)/10), GRAPHICS_NORMAL);          // tens
    dmd.drawChar(17,0, '0'+(counter%10), GRAPHICS_NORMAL);                                // units
There's lots of good stuff here in the forums if you get stuck too.

Cheers ! Geoff

Re: Arduino Counter

Posted: Mon Apr 21, 2014 4:49 pm
by jsmiller12009
Thanks a bunch, Geoff. The first one worked great!