The Japanino, POV Arithmetic Sketch

The Japanino presents a slight challenge when doing program debug - there's no easy way to display numbers to show if the program is working right.  It is possible to use the "serial" library call to send data to the serial port and then display it via the serial monitor in the IDE program.  But, if you have the POV, at a minimum you can send data to the LED stick and display it as the first 7 bits in binary format.  I decided to play with this idea to make a new pattern displayer for the POV.  This sketch randomly chooses between add, subtract, multiply and divide, then randomly chooses two numbers before doing the operation.  The POV displays specific pattens to indicate which operation is being performed and also shows the result.  Nothing really earth-breaking, but if you're trying to learn how to write sketches, this is another example that you can challenge yourself with.  Again, I'm deliberately leaving the comments out.  If you want examples of "good programming practices", check out the Arduino site.

 

----------------------------------------------------------------

 

//  POV Arithmetic Sketch

// May 30, 2010, by Curtis H. Hoffmann

// ThreeStepsOverJapan

 

#define LEDPIN   7       // Start LED numbering with pin D7
#define NUMLED   7       // LED count
#define SWPIN    6       // Switch on pin d6

#define PAT_1010 0b01010101
#define PAT_232  0b01100011
#define PAT_313  0b01110111
#define PAT_34   0b01110000

#define NORM     0
#define INVERT   1
#define FORWARD  0
#define BACKWARD 1

byte bitmap[] = {

  0b01000001,  // 0  X in
  0b00100010,  // 1
  0b00010100,  // 2
  0b00001000,  // 3
  0b00010100,  // 4
  0b00100010,  // 5
  0b01000001,  // 6
  0b01001000,  // 7
  0b00100100,  // 8
  0b00010001,  // 9
  0b00001000,  // 10
};

boolean play_tone = 0;
long randNumber;
byte math_type;

void setup() {
  randomSeed(analogRead(0));
 
  for(char pin = LEDPIN; pin < LEDPIN + NUMLED; pin++) {
    pinMode(pin, OUTPUT);
  }
  pinMode(SWPIN, INPUT);
  digitalWrite(SWPIN, HIGH);
}

void loop() {
   math_type = random(4);
   switch(math_type) {
      case 0:
         add_2(random(120)+5, random(120)+5);
         break;
      case 1:
         subtract_2(random(120)+5, random(120));
         break;
      case 2:
         multiply_2(random(11), random(11));
         break;
      case 3:
         divide_2(random(128), random(30)+1);
         break;
   }
}


void add_2(int n1, int n2) {
   display(n1, FORWARD, NORM);
   delay(2000);
   right(1, FORWARD, NORM);
   display(n2, FORWARD, NORM);
   delay(2000);
   blink_pat(10, PAT_1010);
   display(n1 + n2, FORWARD, NORM);
   delay(2000);
   play_pat( 5,  7, 9, FORWARD, NORM);
}

void subtract_2(int n1, int n2) {
   display(n1, FORWARD, NORM);
   delay(2000);
   right(1, FORWARD, NORM);
   display(n2, FORWARD, NORM);
   delay(2000);
   blink_pat(10, PAT_232);
   display(n1 - n2, FORWARD, NORM);
   delay(2000);
   play_pat( 5,  7, 9, FORWARD, NORM);
}

void multiply_2(int n1, int n2) {
   display(n1, FORWARD, NORM);
   delay(2000);
   right(1, FORWARD, NORM);
   display(n2, FORWARD, NORM);
   delay(2000);
   play_pat( 3,  0, 6, FORWARD, NORM);
   display(n1 * n2, FORWARD, NORM);
   delay(2000);
   play_pat( 5,  7, 9, FORWARD, NORM);
}

void divide_2(int n1, int n2) {
   display(n1, FORWARD, NORM);
   delay(2000);
   right(1, FORWARD, NORM);
   display(n2, FORWARD, NORM);
   delay(2000);
   play_pat( 3,  0, 6, FORWARD, INVERT);
   display(n1 * n2, FORWARD, NORM);
   delay(2000);
   play_pat( 5,  7, 9, FORWARD, NORM);
}

void play_pat(int cnt, int j_start, int j_stop, boolean dir, boolean inv) {
  for(int j2=0; j2<cnt; j2++) {
    if(j_start < j_stop) {
       for(int j1=j_start; j1<=j_stop; j1++) {
          display(bitmap[j1], dir, inv);
       }
     }
     if(j_stop > j_start) {
         for(int j1=j_stop; j1>=j_start; j1--) {
           display(bitmap[j1], dir, inv);
        }
     }
  }
}

void blink_pat(int cnt, int val) {
  for(int j1=0; j1< cnt; j1++) {
    display(val, FORWARD, NORM);
    display(val, FORWARD, INVERT);
  }
}

void right(int cnt, boolean dir, boolean inv) {
  for(int j1=0; j1<cnt; j1++) {
     for (int j2=0; j2<NUMLED; j2++) {
        display(0b01 << j2, dir, inv);
     }
  }
}

void display(byte dval, boolean disp_back, boolean invert) {
   byte temp  = dval;
   byte temp2 = 0;
   int  i;
   unsigned long time = millis();
  
   if(invert) {
      temp = ~temp;  // Invert all 1's to 0's and 0's to 1's.
   }
  
   if(disp_back) {  // Flip the number to display from left to right
      for(i = 0; i < NUMLED; i++) {
        temp2 += ((temp >> i) & 0b00000001) * (64/( 0b00000001 << i));
      }
      temp = temp2;
   }
  
   for(i = 0; i < NUMLED; i++) {
      digitalWrite(LEDPIN + NUMLED - 1 - i, (temp >> i) & 0b00000001);
   }

   if(play_tone) {
      tone(14, temp*25 + 100, 100);
   }
  
   while(millis() - time < 70) {
      if(digitalRead(SWPIN) == LOW) {  // If switch D6 closed, toggle SOUND ON
         play_tone =! play_tone;
         delay(5);
      }
   }
}

------------------------------------------------------

 

Return to the main Index