powerLD_MCL.ino


//powerLD_MCL

#include <LiquidCrystal.h>

const byte interruptPin = 2;
const byte on = 7;
const long interval = 2100;
unsigned long previousMillis = 0;
int stringDat1[300];
int binTab[8] = {0, 32, 16, 8, 4, 2, 1};
int lastSource[5];
int vaitF = 0;
int run = 0;
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];
char stringDatPos2[4];
char stringDatPrint[20];
char str[20];
char str2[10] = "        ";
char stringDatPrint2[10];
volatile int res = 0;
volatile int res0 = 0;
volatile int res1 = 0;
volatile int res2 = 0;
volatile int res3 = 0;
volatile int res4 = 0;
volatile int res5 = 0;
volatile int pos1 = 0;
volatile int pos2 = 0;
volatile int pos3 = 0;
volatile int count = 0;
volatile int countB = 0;
volatile byte last = 1;
volatile byte stop = 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);
}

void 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 (pos1 == 1 || pos1 == 17 || pos1 == 18 || pos1 == 19 || pos1 == 20)
        {
        if (pos1 > 1)
          {
          lcd.setCursor(0, 0);
          lcd.print (str2);
          strcpy (stringDatPrint, stringDatD);
          lcd.setCursor(0, 0); 
          lcd.print(stringDatPrint);
          /*if (pos1 == 18 && pos2 > 0)//CD Nr. ?
            {
            strcpy (stringDatPrint, stringDatD);
            sprintf (str, "%d", pos2);
            strcat (stringDatPrint, str);
            strcat (stringDatPrint, str2);
            lcd.setCursor(0, 0);
            lcd.print(stringDatPrint);
            }*/
          }
      else      
        {
        strcpy (stringDatPrint, stringDatD);
        sprintf (str, "%d", pos2);
        strcat (stringDatPrint, str);
        strcat (stringDatPrint, str2);
        lcd.setCursor(0, 0);
        lcd.print(stringDatPrint);
        }
      Serial.print (stringDatPrint);
      lcd.setCursor(0, 1);
      lcd.print (str2);
      sprintf (str, "%d", pos3);
      strcpy (stringDatPrint2, str);
      lcd.setCursor(2, 1);
      lcd.print(stringDatPrint2);
      Serial.println (stringDatPrint2);
      }

    if (pos1 == 2 || pos1 == 4 || pos1 == 6 || pos1 == 32 || pos1 == 33)//sound
      {
      lcd.setCursor(0, 0);
      lcd.print (str2);
      strcpy (stringDatPrint, stringDatD);
      strcat (stringDatPrint, stringDatD2);
      lcd.setCursor(0, 0);
      lcd.print(stringDatPrint);
      Serial.print (stringDatPrint);
      lcd.setCursor(0, 1);
      lcd.print (str2);
      if (pos1 == 2 || pos1 == 4 || pos1 == 6)
        {
        sprintf (str, "%d", pos3);
        strcpy (stringDatPrint2, str);
        lcd.setCursor(2, 1);
        lcd.print(stringDatPrint2);
        Serial.println (stringDatPrint2);
        }
      }        
    }

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

  startMillis = millis(); 
  if (diffMillis > 13 && diffMillis < 17)//start
      count = 0;
  if (diffMillis > 300)
      countB = 0;
  count ++;
  if (count > 200)
    count = 0;
  countB ++;
  if (countB > 200)
    countB = 0;
  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 (diffMillis > 11 && diffMillis < 14)//stop
    {
    stop = 1;
    }  
}
 
void loop() {
  if (stop)
  {   
    //Serial.println(countB);
    int i = 0;
    /*for (i = 1 ; i < 91; i++)
      {
        Serial.print(stringDat1[i]);
      }*/
    res = 0;
    for (i = 39 ; i < 45; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 38];
      }
      res0 = res; 
    res = 0;
    for (i = 12 ; i < 18; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 11];
      }
      res1 = res;
    res = 0;
    for (i = 36 ; i < 42; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 35];     
      }
      res3 = res;
      digitalWrite (on, HIGH);
    res = 0;
    for (i = 24 ; i < 30; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 23];     
      }
      res4 = res;
    res = 0;
    for (i = 33 ; i < 37; i++)
      {
      if (stringDat1[i] == 1)
        res = res + binTab[i - 30];     
      }
      res5 = res; 
    digitalWrite (on, LOW);
      {     
      if (countB == 45)
        {
        pos1 = res1;
      if (res4 == 0)
        pos2 = res3;
      else
        pos3 = res3;
        //res1 = 0;
        res3 = 0;
        decode (pos1, pos2, pos3);        
        }
      if ((res1 == 1  && countB == 90) || (res1 == 1 && countB == 180))
      //if (res1 == 1  && countB == 90)
        {
        pos1 = res1;
      if (res4 == 0)
        pos2 = res3;
        res3 = 0;
        decode (pos1, pos2, pos3);
        }
      if (res1 == 12 || res1 == 14 || res1 == 8)
        {
      if (countB == 52)//Sound
        {     
        if (stringDat1[45] == 1)
          pos1 = 33;
        else
          pos1 = 32;
        print = 1;
        }
      if (countB == 40)//Bass Treble
        {
        pos1 = res4;
      if (res5 == 0)
        pos2 = 37;
      if (res5 > 9)
        {
        res5 = 16 - res5;
        pos1 = pos1 - 1;
        pos2 = 39;
        }
      else
        pos2 = 38;
      if (res5 == 0)
        pos2 = 37;
      pos3 = res5; 
        print = 1;
        }
      if (countB == 48)//Ball
        {     
        pos1 = res4;
      if (res0 == 0)
        pos2 = 37;
      if (res0 > 56)
        {
        res0 = 64 - res0;
        pos2 = 39;
        }
      else
        pos2 = 38;
      if (res0 == 0)
        pos2 = 37;
      pos3 = res0;
      print = 1; 
        }

      if (print)
        {       
        decode (pos1, pos2, pos3);
        pos2 = 0;
        print = 0;
        }
      }
      }   
    stop = 0;   
  }    
}

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.