Arduino Night: Difference between revisions

From Unallocated Space
Jump to navigation Jump to search
(Created page with "= Random Code =")
 
Line 1: Line 1:
= Random Code =
==Random Code==
* Writing an arbitrary number of bits to pins
  byte pins[] = {2,3,4,5,6,7};
  byte totalPins = 6;
  void setup(){
      for(byte a = 0; a < totalPins; a++){
        pinMode(pins[a], OUTPUT);
      }
  }
 
  void writeData(byte data, byte numberOfBits, byte startBit){
      for(byte a = startBit; a < numberOfBits; a++){
        if((data >> a) & 0x01){
            digitalWrite(pins[a], HIGH);
        }else{
            digitalWrite(pins[a], LOW);
        }
      }
  }

Revision as of 17:47, 29 January 2013

Random Code

  • Writing an arbitrary number of bits to pins
  byte pins[] = {2,3,4,5,6,7};
  byte totalPins = 6;
  void setup(){
     for(byte a = 0; a < totalPins; a++){
        pinMode(pins[a], OUTPUT);
     }
  }
  void writeData(byte data, byte numberOfBits, byte startBit){
     for(byte a = startBit; a < numberOfBits; a++){
        if((data >> a) & 0x01){
           digitalWrite(pins[a], HIGH);
        }else{
           digitalWrite(pins[a], LOW);
        }
     }
  }