Close

code for arduino

A project log for K.I.T.T. - KNIGHT RIDER badge/brooch

whowhow. whowhow. - formally known as purple rider

davedarkodavedarko 10/01/2017 at 21:050 Comments
/*
  Based on https://github.com/maltesemanTS1/Charlieplexing-the-Arduino
  http://arduino.cc/en/Tutorial/BitMask
*/
const int LED_1 = 0;     //LED row 1
const int LED_2 = 1;     //LED row 2
const int LED_3 = 2;     //LED row 3
const int LED_4 = 3;     //LED row 3
#define PRADC 0
#define PRR _SFR_IO8(0x25)
void setup()
{
  // not sure anymore, attiny13?
  // PRR |= (uint8_t)(1 << PRADC);
  // not sure anymore, attiny45?  
  // ADCSRA &= ~(1<<ADEN); // disable adc
}
void loop() {
  for (int i = 0; i < 8; i++) {
    charlie(i);
    delay(50);
  }
  for (int i = 0; i < 8; i++) {
    charlie(7-i);
    delay(50);
  }
  charlie(-1);
  delay(1000);
}
void charlie (int var) {
  pinMode(LED_1, INPUT);
  digitalWrite(LED_1, LOW);
  pinMode(LED_2, INPUT);
  digitalWrite(LED_2, LOW);
  pinMode(LED_3, INPUT);
  digitalWrite(LED_3, LOW);
  pinMode(LED_4, INPUT);
  digitalWrite(LED_4, LOW);
  switch (var) {
    case 0:
      pinMode(LED_1, OUTPUT);
      digitalWrite(LED_1, HIGH);
      pinMode(LED_2, OUTPUT);
      digitalWrite(LED_2, LOW);
      break;
    case 1:
      pinMode(LED_1, OUTPUT);
      digitalWrite(LED_1, LOW);
      pinMode(LED_2, OUTPUT);
      digitalWrite(LED_2, HIGH);
      break;
    case 2:
      pinMode(LED_2, OUTPUT);
      digitalWrite(LED_2, HIGH);
      pinMode(LED_3, OUTPUT);
      digitalWrite(LED_3, LOW);
      break;
    case 3:
      pinMode(LED_2, OUTPUT);
      digitalWrite(LED_2, LOW);
      pinMode(LED_3, OUTPUT);
      digitalWrite(LED_3, HIGH);
      break;
    case 4:
      pinMode(LED_3, OUTPUT);
      digitalWrite(LED_3, HIGH);
      pinMode(LED_4, OUTPUT);
      digitalWrite(LED_4, LOW);
      break;
    case 5:
      pinMode(LED_3, OUTPUT);
      digitalWrite(LED_3, LOW);
      pinMode(LED_4, OUTPUT);
      digitalWrite(LED_4, HIGH);
      break;
    case 6:
      pinMode(LED_1, OUTPUT);
      digitalWrite(LED_1, HIGH);
      pinMode(LED_4, OUTPUT);
      digitalWrite(LED_4, LOW);
      break;
    case 7:
      pinMode(LED_1, OUTPUT);
      digitalWrite(LED_1, LOW);
      pinMode(LED_4, OUTPUT);
      digitalWrite(LED_4, HIGH);
      break;
    default:
      ;
  }
}

Discussions