2013-2014 TEI3M.

CIRC-01 Mod 2. NybbleCounter Code Progression

In class yesterday we looked at an entry level solution to the problem of cycling through the binary presentation of the decimal numbers from 0 to 15. Off the top of my head I attributed a mark of 5/10 to the strategy. Since I want you to acquire great coding skills while you are studying at RSGC, I felt it would be instructive if I developed a progression of possible solutions from 5/10 up to what I would consider worthy of 10/10.

Notes:

  1. Only part of a project's evaluation is for software so don't fret if you're not up to speed yet.
  2. The Arduino IDE Edit menu offers a Copy as HTML feature which I've exploited.

Here they are,

5/10

/**
 * PURPOSE:  
 * AUTHOR:
 * DATE:
 * REF: http://darcy.rsgc.on.ca/ACES/TEI3M/1314/CircuitMods.html#Blink  
 */
int eight = 11;
int four = 8;
int two = 6;
int one = 3;
int duration = 200;
int limit = 16;

void setup(){
  pinMode(eight, OUTPUT);
  pinMode(four, OUTPUT);
  pinMode(two, OUTPUT);
  pinMode(one, OUTPUT);
}

void loop(){
  for(int number=0; number<limit; number++) {
    if(number == 0)
      display(0,0,0,0);
    else if ( number == 1)
      display(0,0,0,1);
    else if ( number == 2)
      display(0,0,1,0);
    else if ( number == 3)
      display(0,0,1,1);
    else if ( number == 4)
      display(0,1,0,0);
    else if ( number == 5)
      display(0,1,0,1);
    else if ( number == 6)
      display(0,1,1,0);
    else if ( number == 7)
      display(0,1,1,1);
    else if ( number == 8)
      display(1,0,0,0);
    else if ( number == 9)
      display(1,0,0,1);
    else if ( number == 10)
      display(1,0,1,0);
    else if ( number == 11)
      display(1,0,1,1);
    else if ( number == 12)
      display(1,1,0,0);
    else if ( number == 13)
      display(1,1,0,1);
    else if ( number == 14)
      display(1,1,1,0);
    else display(1,1,1,1);
    // and so on...
    delay(duration);
  }
}
void display(int bit3, int bit2, int bit1, int bit0){
  digitalWrite(eight, bit3);
  digitalWrite(four, bit2);
  digitalWrite(two, bit1);
  digitalWrite(one, bit0);
}


6/10

/**
 * PURPOSE:
 * AUTHOR: 
 * DATE:
 * COMMENT: 
 */
int eight = 11;
int four = 8;
int two = 6;
int one = 3;
int duration = 200;
int limit = 16;

void setup(){
  pinMode(eight, OUTPUT);
  pinMode(four, OUTPUT);
  pinMode(two, OUTPUT);
  pinMode(one, OUTPUT);
}

void loop(){
  for(int i=0; i<limit; i++) {
    int number = i;
    // Bit 3?
    if( number >=8 ){
      digitalWrite(eight,HIGH);
      number -= 8;
    } 
    else
      digitalWrite(eight,LOW);
    // Bit 2?
    if( number >=4 ){
      digitalWrite(four,HIGH);
      number -= 4;
    } 
    else
      digitalWrite(four,LOW);
    // Bit 1;
    if( number >=2 ){
      digitalWrite(two,HIGH);
      number -= 2;
    } 
    else
      digitalWrite(two,LOW);
    // Bit 0?
    if( number == 1 ){
      digitalWrite(one,HIGH);
    } 
    else
      digitalWrite(one,LOW);



    delay(duration);
  }
}


7/10

/**
 * PURPOSE:  
 * AUTHOR:
 * DATE:
 * COMMENT: 
 */
int eight = 11;
int four = 8;
int two = 6;
int one = 3;
int duration = 200;
int i = 0;
int number;
int limit = 16;

void setup(){
  pinMode(eight, OUTPUT);
  pinMode(four, OUTPUT);
  pinMode(two, OUTPUT);
  pinMode(one, OUTPUT);
}

void loop(){
  number = i;
  // Bit 3?
  if( number >=8 ){
    digitalWrite(eight,HIGH);
    number -= 8;
  } 
  else
    digitalWrite(eight,LOW);
  // Bit 2?
  if( number >=4 ){
    digitalWrite(four,HIGH);
    number -= 4;
  } 
  else
    digitalWrite(four,LOW);
  // Bit 1;
  if( number >=2 ){
    digitalWrite(two,HIGH);
    number -= 2;
  } 
  else
    digitalWrite(two,LOW);
  // Bit 0?
  if( number == 1 ){
    digitalWrite(one,HIGH);
  } 
  else
    digitalWrite(one,LOW);
  delay(duration);
  // modular advance of the number 
  i = (i+1) % limit;
}


8/10

/**
 * PURPOSE:
 * AUTHOR:
 * DATE:
 * COMMENT: 
 */
int bits [] = {
  11,8,6,3};
int pows [] = {
  8,4,2,1};
int duration = 200;
int number = 0;
int working;
int limit = 16;

void setup(){
  for(int i=0; i<=3; i++)
    pinMode(bits[i], OUTPUT);
}

void loop(){
  working = number;
  for(int i=0; i<=3; i++){
    if (working >= pows[i]){
      digitalWrite(bits[i],HIGH);
      working -= pows[i];
    } 
    else
      digitalWrite(bits[i],LOW);
  }

  delay(duration);
  // modular advance of the number 
  number = (number+1) % limit;
}


9/10

/**
 * PURPOSE: 
 * AUTHOR:
 * DATE:
 * COMMENT: 
 */
int bits [] = {
  11,8,6,3};
int pows [] = {
  8,4,2,1};
int duration = 200;
int number = 0;
int limit = 16;

void setup(){
  for(int i=0; i<=3; i++)
    pinMode(bits[i], OUTPUT);
}

void loop(){

  for(int i=0; i<=3; i++){
    if ((number & pows[i]) > 0)
      digitalWrite(bits[i],HIGH);
    else
      digitalWrite(bits[i],LOW);
  }
  delay(duration);
  // modular advance of the number 
  number = (number+1) % limit;
}


10/10

/**
 * PURPOSE:
 * AUTHOR:
 * DATE:
 * COMMENT: 
 */
int bits [] = {
  11,8,6,3};
int duration = 200;
int number = 0;
int limit = 16;

void setup(){
  for(int i=0; i<=3; i++)
    pinMode(bits[i], OUTPUT);
}

void loop(){
  for(int i=0; i<=3; i++)
     digitalWrite(bits[i],number & 1<<(3-i));
  delay(duration);
  // modular advance of the number 
  number = (number+1) % limit;
}