powerLD_MCL_2040_matrix_Doff.ino


//powerLD_MCL_2040_matrix_Doff


#include <max7219.h>
#define SELECT_PIN 3
#define CLOCK_PIN 4
#define DATA_PIN 1

const byte interruptPin = 2;
const byte on = 7;
const long interval = 2100;
unsigned long previousMillis = 0;
bool onR = false; 
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] = "BL";
char stringBass[10] = "BA";
char stringTreble[10] = "TR";
char stringLoudOff[10] = "LO OF";
char stringLoudOn[10] = "LO ON";
char vol[5] = " V ";
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];
char clearDat[7] = "     ";
//char cTemp[32];
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;
int countP = 0;
volatile byte last = 1;
volatile byte stop = 1;
volatile byte print = 0;
volatile byte printMatr = 0;
volatile byte printMatr2 = 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;



void setup() {
Serial.begin(115200);
maxInit(4, 0, CLOCK_PIN, DATA_PIN, SELECT_PIN); // initialize (turn on) the controllers
pinMode(interruptPin, INPUT);
pinMode(on, INPUT);
startMillis = millis();
attachInterrupt(digitalPinToInterrupt(interruptPin), powerL, FALLING);
}

void setup1() {

}

void printMatrix (char stringDatPrint[20]) {
int i, j, iPitch;
  char cTemp[32];
  byte bImg[24 * 8];

  iPitch = 12;
  memset(bImg, 0, iPitch*8);
 
  {
     sprintf(cTemp, stringDatPrint);
     maxDrawString(cTemp, bImg, iPitch, 1); // draw digits into "image" buffer
     for (j=0; j<iPitch*8; j++)
     {  
        maxSendImage(bImg, iPitch);
        maxScrollBitmap(bImg, iPitch, 1);
        delay(40);
     } // for j
  }
}

void printMatrix2 (char stringDatPrint[20]) {
int i, j, iPitch;
  char cTemp[32];
  byte bImg[24 * 8];

  iPitch = 8;
  memset(bImg, 0, iPitch*8);
 
  {
     sprintf(cTemp, stringDatPrint);
     maxDrawString(cTemp, bImg, iPitch, 1); // draw digits into "image" buffer
     for (j=0; j<iPitch*8; j++)
     {  
        maxSendImage(bImg, iPitch);
        //maxScrollBitmap(bImg, iPitch, 1);
        delay(10);
     } // for j
  }
}

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 && pos1 != 18)
          {
          strcpy (stringDatPrint, stringDatD);         
          sprintf (str, "%d", pos3);
          strcat (stringDatPrint, vol);
          strcat (stringDatPrint, str);
          printMatr = 1;
          Serial.print (stringDatPrint);
          }
        else if (pos1 == 18)   
          {
          Serial.println (pos2);       
          strcpy (stringDatPrint, stringDatD);         
          sprintf (str, "%d", pos2);
          sprintf (str2, " %d", pos3);
          strcat (stringDatPrint, str);
          strcat (stringDatPrint, vol);
          strcat (stringDatPrint, str2);
          printMatr = 1;
          Serial.print (stringDatPrint);
          }
        else
          {       
          strcpy (stringDatPrint, stringDatD);
          if (pos2 > 0)
          {
          sprintf (str, "%d", pos2);
          sprintf (str2, " %d", pos3);
          strcat (stringDatPrint, str);
          strcat (stringDatPrint, vol);
          strcat (stringDatPrint, str2);
          printMatr = 1;
          Serial.print (stringDatPrint);
          pos2 = 0;
          }
        }
      }

    if (pos1 == 32 || pos1 == 33)
      {
      strcpy (stringDatPrint2, stringDatD);
      printMatr2 = 1;
      Serial.print (stringDatPrint2);
      }
    if (pos1 == 2)
      {
      strcpy (stringDatPrint2, stringDatD);
      strcat (stringDatPrint2, stringDatD2);
      sprintf (str, "%d", pos3);
      strcat (stringDatPrint2, str);
      printMatr2 = 1;
      Serial.print (stringDatPrint2);
      }
    if (pos1 == 4)
      {
      strcpy (stringDatPrint2, stringDatD);
      strcat (stringDatPrint2, stringDatD2);
      sprintf (str, "%d", pos3);
      strcat (stringDatPrint2, str);
      printMatr2 = 1;
      Serial.print (stringDatPrint2);
      }
    if (pos1 == 6)
      {
      strcpy (stringDatPrint2, stringDatD);
      strcat (stringDatPrint2, stringDatD2);
      sprintf (str, "%d", pos3);
      strcat (stringDatPrint2, str);
      printMatr2 = 1;
      Serial.print (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
    {
    onR = digitalRead (on);
    stop = 1;
    if (onR == false)
    {
      strcpy (stringDatPrint, clearDat);
      printMatrix (stringDatPrint);
      Serial.println ("notOn");
    }
    }  
}
 
void loop() {
 

  if (stop)
  {   
    int i = 0;
    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);
        pos2 = 0;
        }
      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 = 35;
        }
      else
        pos2 = 36;
      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;   
  }    
}

void loop1() {
if (printMatr)
    {
      printMatrix (stringDatPrint);
      printMatr = 0;
    }
  if (printMatr2)
    {
    printMatrix2 (stringDatPrint2);
    printMatr2 = 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.