#include <Arduino.h>
int main(void) {
init();
#if defined(USBCON)
USBDevice.attach();
#endif
//==================== PINMODE registers (INPUT unless told otherwise)
//
// PORTA -->
// - A0 - SC0 - 22
// - A1 - BK0 - 23
// - A2 - SC1 - 24
// - A3 - BK1 - 25
// - A4 - SC2 - 26
// - A5 - BK2 - 27
// - A6 - SC3 - 28
// - A7 - BK3 - 29
//
// PORTJ --> NOTE --> The majority of the bits in the PORTJ do NOT have a pinout in the Arduino MEGA board
// - J6 - SC4
// - J5 - BK4
// - J4 - SC5
// - J3 - BK5
// - J2 - SC6
// - J1 - BK6
//
// PORTC -->
// - C7 - SC7 - 30
// - C6 - BK7 - 31
// - C0 - DIR_485
//
// PORTD -->
// - D3 - TX1
// - D2 - RX1
DDRC = DDRC | (1 << DDC0); //The only output register we need to control is PORTC0
Serial.begin (9600);
PORTC = PORTC | (1<<PORTC0);
uint8_t counter_A = 0;
uint8_t free_bikes = 0;
uint8_t free_scooters = 0;
uint8_t counter_J = 0;
char scooters{};
char bikes{};
for (;;) { //For loop
counter_A = 0;
free_bikes = 0;
free_scooters = 0;
for(uint32_t iter = 1; iter < 0b10000001; iter = (iter << 1)) {
if(PINA & iter) {
// The position we are checking is 1, so its NOT free. We do nothing.
++counter_A;
} else {
// The position we are checking is 0, so its FREE.
if(counter_A % 2) {
// If the counter is odd, the current reading is from a BIKE that is free
++free_bikes;
} else {
// If the counter is even, the current reading is from a SCOOTER that is free
++free_scooters;
}
++counter_A;
}
}
counter_J = 0;
for(uint32_t iter = 2; iter < 0b01000001; iter = (iter << 1)) {
if(PINJ & iter) {
// The position we are checking is 1, so its NOT free. We do nothing.
++counter_J;
} else {
// The position we are checking is 0, so its FREE.
if(counter_J % 2) {
// If the counter is odd, the current reading is from a BIKE that is free
++free_bikes;
} else {
// If the counter is even, the current reading is from a SCOOTER that is free
++free_scooters;
}
++counter_J;
}
}
if(!(PINC & (1<<PIN7))) {
//If PIN7 of PORTC is 0, it means its free (last remainding scooter stand).
++free_scooters;
}
if(!(PINC & (1<<PIN6))) {
//If PIN6 of PORTC is 0, it means its free (last remainding bike stand).
++free_bikes;
}
scooters = free_scooters + 48;
bikes = free_bikes + 48;
char message[5] = {bikes, ' ', '-', ' ', scooters}; // '\0' --> String conversion
Serial.println(message);
delay (100);
if (serialEventRun) serialEventRun();
}
return 0;
}