powerL_D.ino
//powerL_D
#include <LiquidCrystal.h>
int i = 0;
int inP = 8;
int binTab[10] = {0, 128, 64, 32, 16, 8, 4, 2, 1};
int res2 = 0;
int res3 = 0;
int res4 = 0;
int res5 = 0;
int resS = 0;
int resV = 0;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int flagS = 0;
int flagV = 0;
int flagSt = 0;
int lastP = 0;
int lastV = 0;
const byte interruptPin = 2;
int stringDat[112];
char stringRadio[8] = "RADIO ";
char stringAtape[8] = "A.TAPE";
char stringCd[8] = "CD ";
char stringPhono[8] = "PHONO ";
char stringAtape2[8] = "A.TP2 ";
char stringBallance[10] = "BALLANCE";
char stringBass[10] = "BASS ";
char stringTreble[10] = "TREBLE ";
char stringLoudOff[10] = "LOUD OFF";
char stringLoudOn[10] = "LOUD ON ";
char stringDatD[10];
volatile byte state = LOW;
volatile int count = 0;
volatile byte last = 0;
volatile byte res = 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);
pinMode(interruptPin, INPUT);
pinMode(inP, INPUT);
startMillis = millis();
attachInterrupt(digitalPinToInterrupt(interruptPin), powerL, FALLING);
}
int decode (int pos1, int pos2, int pos3)
{
switch (pos1)
{
case 11 : {//RADIO
strcpy (stringDatD, stringRadio);
lastP = pos2;
}
break;
case 139 : {//A.TAPE
strcpy (stringDatD, stringAtape);
lastP = pos2;
}
break;
case 147 : {//CD
strcpy (stringDatD, stringCd);
lastP = pos2;
}
break;
case 155 : {//PHONO
strcpy (stringDatD, stringPhono);
lastP = pos2;
}
break;
case 163 : {//A.TP2
strcpy (stringDatD, stringAtape2);
lastP = pos2;
}
break;
case 129 : {//BALL
strcpy (stringDatD, stringBallance);
lastP = 0;
}
break;
case 130 : {//BASS
strcpy (stringDatD, stringBass);
lastP = 0;
}
break;
case 131 : {//TREBLE
strcpy (stringDatD, stringTreble);
lastP = 0;
}
break;
case 132 : {//LOUD
strcpy (stringDatD, stringLoudOff);
lastP = 0;
}
break;
case 117: {//VOL
lastV = pos3;
}
break;
default: {
return 0;
}
}
Serial.print(stringDatD);//Skriver til serial monitor
Serial.print("\t");
Serial.print(pos2);
Serial.print("\t");
Serial.print(pos3);
Serial.println();
return 0;
}
void powerL() {
endMillis = millis();
diffMillis = endMillis - startMillis;
if (count > 0 && diffMillis > 80)//test for gab
{
count = 0;
}
startMillis = millis();
count ++;
if (count > 104)
count = 0;
if (count == 5)
last = 1;
if (count > 5)
{
if (last == 1 && diffMillis > 4 && diffMillis < 8)
{
stringDat[count] = 1;
last = 1;
}
else if (last == 0 && diffMillis > 8)
{
stringDat[count] = 1;
last = 1;
}
else
{
stringDat[count] = 0;
last = 0;
}
}
if (count == 39)
{
res4 = 0;
resS = 0;
pos2 = res5;
for (i = 24; i < 32; i++)
{
if (stringDat[i] == 1)
res4 = res4 + binTab[i - 23];
}
if (res4 == 128)
{
flagS = 0;
pos1 = res3;
decode (pos1, pos2, pos3);
}
if (res4 > 128 && res4 < 133)
{
flagS = 1;
for (i = 36; i < 40; i++)
{
if (stringDat[i] == 1)
resS = resS + binTab[i - 31];
}
}
if (resS > 10)
{
resS = 16 - resS;
pos2 = 35;
}
else if (resS < 10)
pos2 = 0;
pos1 = res4;
pos3 = resS;
decode (pos1, pos2, pos3);
}
if (count == 51)
{
flagV = 0;
res = 0;
for (i = 13; i < 21; i++)
{
if (stringDat[i] == 1)
res = res + binTab[i - 12];
}
if (res == 78)
{
resV = 0;
flagV = 1;
for (i = 41; i < 48; i++)
{
if (stringDat[i] == 1)
resV = resV + binTab[i - 39];
}
decode (pos1, pos2, pos3);
}
}
if (count == 53)
{
res3 = 0;
if (flagV == 0)
res5 = 0;
for (i = 16; i < 24; i++)
{
if (stringDat[i] == 1)
res3 = res3 + binTab[i - 15];
}
if (flagV == 0)
{
for (i = 37; i < 45; i++)
{
if (stringDat[i] == 1)
res5 = res5 + binTab[i - 36];
}
if (res5 == 0)
flagSt = 1;
else
flagSt = 0;
}
if (res3 == 11 || res3 == 147 || res3 == 155 || res3 == 139 || res3 == 163)
{
flagS = 0;
pos1 = res3;
pos2 = res5;
pos3 = resV;
decode (pos1, pos2, pos3);
}
res2 = 0;
}
}
void loop() {
if (flagS == 1)
{
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(stringDatD);
lcd.setCursor(5, 0);
if (pos2 == 35)
{
lcd.print("-");
lcd.print(pos3);
lcd.print(" ");
}
else
{
lcd.print(" ");
lcd.print(pos3);
lcd.print(" ");
}
}
if (flagS == 0)
{
lcd.setCursor(0, 0);
lcd.print(stringDatD);
if (lastP < 10)
lcd.print(" ");
lcd.print(lastP);
lcd.setCursor(0, 1);
lcd.print(" VOL");
lcd.print(" ");
if (resV < 10)
lcd.print(" ");
lcd.print(resV);
lcd.print(" ");
}
if (flagSt == 1)
{
lcd.setCursor(0, 0);
lcd.print("STANDBY ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
delay (100);
}
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.