powerLD_v2.ino

//powerLD_v2
#include <LiquidCrystal.h>

const byte interruptPin = 2;
const byte on = 7;
int stringDat1[112];
int binTab[8] = {0, 32, 16, 8, 4, 2, 1};
int lastSource[5];
char stringRadio[8] = "RADIO ";
char stringAtape[8] = "ATAPE ";
char stringCd[8] = "CD    ";
char stringPhono[8] = "PHONO ";
char stringAtape2[8] = "A.TP2 ";
char stringBallance[10] = "BALL  ";
char stringBass[10] = "BASS    ";
char stringTreble[10] = "TREB    ";
char stringLoudOff[10] = "LOUD OFF";
char stringLoudOn[10] = "LOUD ON ";
char stringNeg[10] = "- ";
char stringPlus[10] = "+ ";
char stringNev[10] = "><";
char stringRight[10] = " >";
char stringLeft[10] = "< ";
char stringMute[10] = "--";
char stringDatD[10];
char stringDatD2[10];
char stringDatD3[10];
volatile int res = 0;
volatile int pos1 = 0;
volatile int pos2 = 0;
volatile int pos3 = 0;
volatile int count = 0;
volatile byte last = 1;
volatile byte print = 0;
volatile byte sound = 0;
volatile byte source = 0;
volatile byte number = 0;
volatile unsigned long startMillis;
volatile unsigned long endMillis;
volatile unsigned long diffMillis;
volatile unsigned long currentMillis;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);// initialisering af LCD

void setup() {
Serial.begin(115200);
lcd.begin(8, 2);
//lcd.print("Starting powerLD");
//delay(3000);
pinMode(interruptPin, INPUT);
pinMode(on, OUTPUT);
startMillis = millis();
attachInterrupt(digitalPinToInterrupt(interruptPin), powerL, FALLING);
}

int decode (int pos1, int pos2, int pos3)
        {
        switch (pos1)
        {
        case 1 : {//RADIO
                  strcpy (stringDatD, stringRadio);
                  }
                  break;
        case 17 : {//A.TAPE
                  strcpy (stringDatD, stringAtape);
                  }
                  break;
           case 18 : {//CD
                  strcpy (stringDatD, stringCd);
                  }
                  break;
        case 19 : {//PHONO
                  strcpy (stringDatD, stringPhono);
                  }
                  break;
        case 20 : {//A.TP2
                  strcpy (stringDatD, stringAtape2);
                  }
                  break;
          case 2 : {//BALL
                  strcpy (stringDatD, stringBallance);
                  }
                  break;
          case 4 : {//BASS
                  strcpy (stringDatD, stringBass);
                  }
                  break;
          case 6 : {//TREBLE
                  strcpy (stringDatD, stringTreble);
                  }
                  break;
          case 32 : {//LOUD
                  strcpy (stringDatD, stringLoudOff);
                  }
                  break;
          case 33 : {//LOUD
                  strcpy (stringDatD, stringLoudOn);
                  }
                  break;
         
          default:  {
                  return 0;
                  }
                }
      if (pos2 >33)
        {
        switch (pos2)
        {
        case 35 : {//Neg
                  strcpy (stringDatD2, stringNeg);
                  }
                  break;
        case 36 : {//Plus
                  strcpy (stringDatD2, stringPlus);
                  }
                  break;
        case 37 : {//><
                  strcpy (stringDatD2, stringNev);
                  }
                  break;
        case 38 : {//>
                  strcpy (stringDatD2, stringRight);
                  }
                  break;
        case 39 : {//<
                  strcpy (stringDatD2, stringLeft);
                  }
                  break;
        case 50 : {//MUTE
                  strcpy (stringDatD3, stringMute);
                  //lastP = pos2;
                  }
                  break;         
        default:  {
                  return 0;
                  }         
        }
      }
        if (pos3 == 40)
          strcpy (stringDatD3, stringMute);
        lcd.setCursor(0, 0);
        lcd.print("       ");
        lcd.setCursor(0, 0);
        lcd.print(stringDatD);
        Serial.print(stringDatD);//Skriver til serial monitor
        Serial.print("\t");
       
        lcd.setCursor(6, 0);
        if (pos2 > 33 && pos1 < 32)
          {
          lcd.print(stringDatD2);
          Serial.print(stringDatD2);
          }
        else if (pos2 < 32)
          {
          lcd.setCursor(6, 0);
          lcd.print("  ");
          lcd.setCursor(6, 0);
          lcd.print(pos2);
          Serial.print(pos2);
          }
        Serial.print("\t");
        if (pos3 == 40)
          {
          lcd.setCursor(2, 1);
          lcd.print(stringDatD3);
          Serial.print(stringDatD3);
          }
        else if (pos2 != 37 && pos1 < 32)
          {
          lcd.setCursor(2, 1);
          lcd.print("   ");
          lcd.setCursor(2, 1);
          lcd.print(pos3); 
          Serial.print(pos3);
          }
        else
          {
          lcd.setCursor(2, 1);
          lcd.print("   ");
          }
        Serial.println();
      return 0;
    }

void powerL() {
  int i = 0;
  endMillis = millis();
  diffMillis = endMillis - startMillis;

  if (diffMillis > 11 && diffMillis < 14 && count == 49)//source + Nummer
    {
    res = 0;
    for (i = 12 ; i < 18; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 11];
      }
      pos1 = res;
      source = res;
      print = 1;
    res = 0;
    for (i = 36 ; i < 42; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 35];     
      }
      pos2 = res;
      number = res;
      sound = 0;
    }   

 

  if (diffMillis > 11 && diffMillis < 14 && count == 48)//Volume
    {
    res = 0;
    for (i = 38 ; i < 44; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 37];
      }
      pos3 = res * 2;
      print = 1;
    }

  if (diffMillis > 11 && diffMillis < 14 && count == 48)//LOUD ON OFF
    {
    if (sound)
    {
    if (stringDat1[45] == 1)//LOUD ON
      pos1 = 33;
    else
      pos1 = 32;
    print = 1;
    }     
    } 

  if (diffMillis > 11 && diffMillis < 14 && count == 48)//return from sound
  {
  if (sound)
  {
  if (stringDat1[26] == 0)
    {
    pos1 = lastSource[1];
    pos2 = lastSource[2];
    }
  }
  if (stringDat1[24] == 1)
    {
    pos3 = 40;
    }
  else
    //pos2 = lastP2;
  print = 1;
  }

  if (diffMillis > 11 && diffMillis < 14 && count == 36)//Sound Bass Treble
    {   
    res = 0;
    for (i = 24 ; i < 29; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 23];
      }
      //Serial.println(res);
      pos1 = res;
      print = 1;
    res = 0;
    for (i = 31 ; i < 37; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 30];     
      }
      if (res > 56)
        {
        res = 64 - res;
        pos2 = 35; 
        }
      else
        pos2 = 36;
      pos3 = res;
      print = 1;
      sound = 1;
    }

  if (diffMillis > 11 && diffMillis < 14 && count == 44)//Sound Ball
    {
    lastSource[1] = source;
    lastSource[2] = number;
    res = 0;
    for (i = 24 ; i < 30; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 23];
      }
      pos1 = res;
      print = 1;
    res = 0;
    for (i = 39 ; i < 45; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 38];     
      }
      if (res == 0)
        pos2 = 37;
      if (res > 56)
        {
        res = 64 - res;
        pos2 = 39; 
        }
      else
        pos2 = 38;
      if (res == 0)
        pos2 = 37;
      pos3 = res;
      print = 1;
      sound = 1;
    }
 
  startMillis = millis(); 
  if (diffMillis > 13 && diffMillis < 17)
      count = 0;
  count ++;
  if (last == 1 && diffMillis > 4 && diffMillis < 8)
      {   
      stringDat1[count] = 1;
      last = 1;
      }
    else if (last == 0 && diffMillis > 8)
      {
      stringDat1[count] = 1;
      last = 1;
      }
    else
      {
      stringDat1[count] = 0;
      last = 0;
      }
  if (print == 1)   
    decode (pos1, pos2, pos3);
  print = 0; 
 
}
void loop() {

}


Compile med: Arduino ide se www.arduino.cc

Bemærk: dette er eksperimentel software der er ingen garanti for brugbarhed, det kan derimod sandsynligvis være skadeligt, kun til brug i Danmark.