Programming the Arduino

This is a collection of links and tips on how to program the Arduino. Main source is the Arduino.cc Code Library.

Push buttons

Sound Generation

Fast Division

fast division by 10

Fast division by 10

void divmod10(uint32_t in, uint32_t &div, uint32_t &mod)
{
  uint32_t x=(in>>1); // div = in/10 <==> div = ((in/2)/5)
        uint32_t q=x;

        q= (q>>8) + x;
        q= (q>>8) + x;
        q= (q>>8) + x;
        q= (q>>8) + x + 1;

        x= q;
        q= (q>>1) + x;
        q= (q>>3) + x;
        q= (q>>1) + x;

        div = (q >> 3);
        mod = in - (((div << 2) + div) << 1);
}

This was the earlier version:

Fast division by 10 early version

void divmod10(uint32_t in, uint32_t &div, uint32_t &mod)
{
  // q = in * 0.8;
  uint32_t q = (in >> 1) + (in >> 2);
  q = q + (q >> 4);
  q = q + (q >> 8);
  q = q + (q >> 16);  // not needed for 16 bit version

  // q = q / 8;  ==> q =  in *0.1;
  q = q >> 3;
  
  // determine error
  uint32_t  r = in - ((q << 3) + (q << 1));   // r = in - q*10; 
  div = q + (r > 9);
  if (r > 9) mod = r - 10;
  else mod = r;
}

Home Automation

Doing Two things at Once

Logic Probe

AJMPublic/teaching/arduino-pi/projects/arduino/programming (last edited 2021-04-14 13:17:38 by apw109)