I had something strange happen while fixing the code
the Leonardo after I had the Arduino IDE compile and upload the pc com4 disappeared from device manager.
it didn't like all my case statements or the multiple Serial.print lines so I trimmed them out what I figured was important.
where is compiler placing all text lines do they become constant character arrays or what?
The PC version works only when connected to PC which I expect
I tried it using a USB power adapter and 12V adapter to the DC input jack (not the headers)
The no pc version fails as well unless it is connected to the PC
int led = 13;
int len;
boolean mainDoorStatus;
boolean cashDoor;
boolean cashBox;
boolean standDoor;
boolean changeLight;
byte globalCRC[2];
void setup()
{
Serial1.begin(19200);
pinMode(led, OUTPUT);
}
void loop()
{
int i;
byte serByte;
byte bArray[50];
int bCount;
byte PC;
byte lastPoll;
byte legacyBonus[9]={
0x01, 0x8A, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0F, 0x8A };
byte gDisable[4]={
0x01, 0x01, 0x51, 0x08 };
byte gEnable[4]={
0x01, 0x02, 0xCA, 0x3A };
UCSR1C = 0b00000110;
UCSR1B = 0b10011101;
Serial1.write(0x80);
delay(1);
UCSR1B = 0b10011101;
Serial1.write(0x81);
delay(1);
UCSR1B = 0b10011100;
delay(10);
i=0;
serByte=0;
if (Serial1.available()>0)
{
serByte=Serial1.read();
}
if (serByte>0)
{
if (serByte != lastPoll)
{
lastPoll = serByte;
}
else serByte=0;
}
switch (serByte)
{
case 0x11:
if (mainDoorStatus==false)
{
digitalWrite(led,HIGH);
mainDoorStatus=true;
}
break;
case 0x12:
if (mainDoorStatus==true)
{
digitalWrite(led,LOW);
mainDoorStatus==false;
}
break;
case 0x13:
if (standDoor==false)
{
digitalWrite(led,HIGH);
standDoor=true;
}
break;
case 0x14:
if (standDoor==true)
{
digitalWrite(led,LOW);
standDoor=false;
}
break;
case 0x19:
if (cashDoor==false)
{
digitalWrite(led,HIGH);
cashDoor=true;
}
break;
case 0x1A:
if (cashDoor==true)
{
digitalWrite(led,LOW);
cashDoor=false;
}
break;
case 0x1B:
if (cashBox==false)
{
digitalWrite(led,HIGH);
cashBox=true;
}
break;
case 0x1C:
if (cashBox==true)
{
digitalWrite(led,LOW);
cashBox=false;
}
break;
case 0x1F:
break;
case 0x3D:
getTicketInfo(0);
break;
case 0x51:
handPay(0);
break;
case 0x57:
printTicket(0);
break;
case 0x67:
redeemTicket(0);
break;
case 0x68:
transferComplete(0);
break;
case 0x71:
if (changeLight==false)
{
digitalWrite(led,HIGH);
changeLight=true;
}
break;
case 0x72:
if (changeLight==true)
{
digitalWrite(led,LOW);
changeLight=false;
sendCommand(legacyBonus,9);
}
break;
case 0x7C:
break;
default:
break;
}
delay(80);
}
void handPay(int du)
{
int i;
byte hp[24];
hp[0]=0x01;
hp[1]=0x1B;
sendCommand(hp,2);
i = 0;
while (i<24)
{
if (Serial1.available()>0)
{
hp[i]=Serial1.read();
i++;
}
}
}
void getTicketInfo(int dummy)
{
int i;
int nByte;
byte reByte;
byte getVal[5]={
0x01, 0x4D, 0x00, 0xC2, 0xAC };
sendCommand(getVal,5);
i=0;
while (i<35)
{
if (Serial1.available()>0)
{
reByte=Serial1.read();
i++;
}
}
}
void printTicket(int dummy)
{
int i;
byte sysVal[13];
sysVal[0]=0x01;
sysVal[1]=0x57;
sendCommand(sysVal,2);
i = 0;
while (i<9)
{
if (Serial1.available()>0)
{
sysVal[i]=Serial1.read();
i++;
}
}
sysVal[1]=0x58;
sysVal[2]=0x01;
for (i=10; i>5; i--)
{
sysVal[i]=sysVal[i-3];
}
for (i=3;i<6;i++)
{
sysVal[i]=0x00;
}
computeCRC(sysVal,11);
sysVal[11]=globalCRC[0];
sysVal[12]=globalCRC[1];
sendCommand(sysVal,13);
i=0;
while (i<5)
{
if (Serial1.available()>0)
{
sysVal[i]=Serial1.read();
i++;
}
}
}
void redeemTicket(int dummy)
{
int i;
byte ticket[21];
ticket[0]=0x01;
ticket[1]=0x70;
sendCommand(ticket,2);
i = 0;
while (i<21)
{
if (Serial1.available()>0)
{
ticket[i]=Serial1.read();
i++;
}
}
ticket[1]=0x71;
for (i=4; i<9; i++)
{
ticket[i]=ticket[i+10];
}
computeCRC(ticket,19);
ticket[19]=globalCRC[0];
ticket[20]=globalCRC[1];
sendCommand(ticket,21);
i=0;
while (i<21)
{
if (Serial1.available()>0)
{
ticket[i]=Serial1.read();
i++;
}
}
byte transComplete[6]={
0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0 };
sendCommand(transComplete,6);
i=0;
while (i<21)
{
if (Serial1.available()>0)
{
transComplete[0]=Serial1.read();
i++;
}
}
}
void transferComplete(int dummy)
{
int i;
byte transComplete[6]={
0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0 };
sendCommand(transComplete,6);
i=0;
while (i<21)
{
if (Serial1.available()>0)
{
transComplete[0]=Serial1.read();
i++;
}
}
}
void sendCommand(byte temp[], int len)
{
UCSR1B = 0b10011101;
Serial1.write(temp[0]);
delay(1);
UCSR1B = 0b10011100;
for (int i=1; i<len;i++)
{
Serial1.write(temp[i]);
delay(1);
}
}
void computeCRC(byte val[], int len)
{
globalCRC[0]=0;
globalCRC[1]=0;
long crc;
long q;
byte c;
crc = 0;
for (int i = 0; i < len; i++)
{
c = val[i];
q = (crc ^ c) & 0x0f;
crc = (crc >> 4) ^ (q * 0x1081);
q = (crc ^ (c >> 4)) & 0xf;
crc = (crc >> 4) ^ (q * 0x1081);
}
globalCRC[0]=crc & 0xff;
globalCRC[1]=crc >> 8;
}