Hey guys
I was trying to get some relay boards connected to ports A/B/C/L of my Arduino mega2560
and turning them on/off according to a message I will be receiving from the PC
and then returning some analog/digital reads to that PC
so i wrote this basic program to do that for me:
#include <SoftwareSerial.h>
#include <avr/io.h>
const byte AnalogInputs[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
int led = 13; //indication LED
int val;
word wordVal;
byte buffer[11];
void setup()
{
Serial.begin(115200);
pinMode (led, OUTPUT);
DDRC = 0xFF; // Relay Board 1
DDRA = 0xFF; // Relay Board 2
DDRB = 0xFF; // Relay Board 3
DDRL = 0xFF; // Relay Board 4
//inputs
DDRG = 0x00;
DDRE = 0x00;
DDRH = 0x00;
DDRD = 0x00;
}
void loop()
{
Serial.readBytes(buffer, sizeof(buffer));
ParseAllRelays(buffer[]);
writeAllAnalog();
delay (1000);
}
void writeAllAnalog(void)
{
word checksum;
byte message[40] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
message[0] = 0xA5;
message[1] = 0x5A;
checksum = message[1] + message[0];
for(int i = 0; i<16 ; i++)
{
val = analogRead(AnalogInputs[i]);
wordVal = word(val);
message[(i+1)*2] = lowByte(wordVal);
message[(i+1)*2+1] = highByte(wordVal);
//checksum = checksum + wordVal;
checksum = checksum + message[(i+1)*2]+message[(i+1)*2+1];
}
message[34] = PORTG;
message[35] = PORTE;
message[36] = PORTH;
message[37] = PORTD;
checksum = checksum + message[34] + message[35] + message[36] + message[37];
message[38] = lowByte(checksum);
message[39] = highByte(checksum);
Serial.write(message, sizeof(message));
}
void ParseAllRelays(byte buffer[])
{
//checksum checker:
word checksum = 0x0000;
for (int i=0 ; i<sizeof(buffer)-2; i++)
checksum = checksum + buffer[i];
if ((buffer[0] == A5) && (buffer[1] == 5A) && (lowByte(checksum) == buffer[9]) && (highByte(checksum) == buffer[10]))
{
PORTC = buffer[2]; // Relay Board 1
PORTA = buffer[3]; // Relay Board 2
PORTB = buffer[4]; // Relay Board 3
PORTL = buffer[5]; // Relay Board 4
}
}
although Im receiving error messages like "note: suggested alternative: 'PORT0'
PORTA = buffer[3]; // Relay Board 2
^~~~~
PORT0
"
and something about A8 not being declared?
how can I combine arduino with AVR code?