Here's the code:
[code]
#include <EEPROM.h>
#define EEPROM_SIZE 1
#define RXD2 16 // Serial1 Pins
#define TXD2 17
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219_A;
Adafruit_INA219 ina219_B(0x41);
float shuntvoltage_A = 0;
float busvoltage_A = 0;
float current_mA_A = 0;
float loadvoltage_A = 0;
float power_mW_A = 0;
float shuntvoltage_B = 0;
float busvoltage_B = 0;
float current_mA_B = 0;
float loadvoltage_B = 0;
float power_mW_B = 0;
//************ SENSOR BOARD START *******************
#define START_BYTES 5
#define LENGTH 98
const byte startMarker[START_BYTES] = {'S', 'B', '1', '.', '0'};
byte receivedBytes[LENGTH];
uint8_t idx = 0;
boolean processData = false;
boolean started = false;
//************ SENSOR BOARD END *******************
uint16_t Leds1 = 45;
uint16_t Leds2 = 30;
uint16_t Leds1a = 0;
#include <ezButton.h>
#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters
ezButton button(5); // create ezButton object that attach to pin GPIO5
ezButton button2(23);
int SerialPeriod = 50; // Serial1 Write interval
unsigned long SerialTime_now = 0;
int inaPeriod = 4000; //INA219 Serial Print Interval
unsigned long inaTime_now = 0;
int LedPeriod = 250; // LED Count Interval
int LedPeriod2 = 500;
int LedPeriod3 = 750;
int LedPeriod4 = 1000;
int LedPeriod5 = 1250;
unsigned long LedTime_now;
unsigned long LedTime_now2;
unsigned long LedTime_now3;
unsigned long LedTime_now4;
unsigned long LedTime_now5;
bool ItWasDone = false;
bool ItWasDone2 = false;
bool ItWasDone3 = false;
bool ItWasDone4 = false;
bool ItWasDone5 = false;
int AlarmPeriod = 15000; // Alarm On
unsigned long AlarmTime_now = 0;
bool AlarmDone = false;
uint16_t Colour1A = 0; // COLOURS!!!
uint16_t Colour1B = 0;
uint16_t Colour1C = 0;
uint16_t Colour1D = 0;
uint16_t Colour2A = 0;
uint16_t Colour2B = 0;
uint16_t Colour2C = 0;
uint16_t Colour2D = 0;
byte header[] = {0x53, 0x42, 0x31, 0x2E, 0x30, 0x00}; //HEADER SB1.0
byte freq[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 1/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 2/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 3/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 4/6
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FREQUENCY 5/6
0x00, 0x00, 0x00, 0x00
}; // FREQUENCY 6/6
byte audio[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // AUDIO ENERGY
byte acc[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //ACCELEROMETER
byte light[] = {0x00, 0x00}; // LIGHT SENSOR
byte analog[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //ANALOG INPUTS
byte end[] = {0x45, 0x4E, 0x44, 0x00}; // END
// *************** POTENTIOMETERS START **********************
const int NINPUTS = 4;
const byte inputPin[NINPUTS] = { 27, 14, 12, 13 };
int reading[NINPUTS];
float smoothedReading[NINPUTS];
void takeReadings() {
for (int i = 0; i < NINPUTS; i++) {
reading[i] = analogRead(inputPin[i]);
smoothedReading[i] = smoothedReading[i] * .85 + reading[i] * .15;
}
}
// ****************** POTENTIOMETERS END ***********************
void setup() {
//setCpuFrequencyMhz(40); // Drop consumption, save the planet:)
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, RXD2, TXD2);
Wire.begin();
ina219_A.begin();
ina219_B.begin();
EEPROM.begin(EEPROM_SIZE);
Leds1 = EEPROM.read(0);
//Leds2 = EEPROM.read(1);
Serial.println(Leds1);
pinMode(18, OUTPUT); // SHUT DOWN TRIGGER
pinMode(5, INPUT_PULLUP); // LED COUNT BUTTON
pinMode(23, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP); // SENSOR BOARD SELECTOR
button.setDebounceTime(DEBOUNCE_TIME);
uint32_t currentFrequency; //INA219
}
void loop() {
takeReadings(); // ANALOG INPUT SMOOTHING (Pots 1-4)
shuntvoltage_A = abs(ina219_A.getShuntVoltage_mV());
busvoltage_A = ina219_A.getBusVoltage_V();
current_mA_A = abs(ina219_A.getCurrent_mA());
power_mW_A = ina219_A.getPower_mW();
loadvoltage_A = busvoltage_A + (shuntvoltage_A / 1000);
shuntvoltage_B = abs(ina219_B.getShuntVoltage_mV());
busvoltage_B = ina219_B.getBusVoltage_V();
current_mA_B = abs(ina219_B.getCurrent_mA());
power_mW_B = ina219_B.getPower_mW();
loadvoltage_B = busvoltage_B + (shuntvoltage_B / 1000);
//if (loadvoltage_A < 16) { // BATTERY LOW WARNING
if (button2.isPressed()) {
Leds2 = Leds2 + 500;
//Leds2 = 2000;
AlarmDone = true;
AlarmTime_now = millis();
}
if (AlarmDone && ( millis() - AlarmTime_now >= AlarmPeriod)) {
Leds1 = EEPROM.read(0);
//Leds2 = EEPROM.read(1);
AlarmDone = false;
}
if (loadvoltage_A < 15) { // BATTERY LOW SHUT DOWN
digitalWrite(18, HIGH);
}
button.loop(); // LED COUNT BUTTON DEBOUNCE ROUTINE
button2.loop();
/*
if (button.isPressed()) {
Leds1 = 400;
//Leds2 = 400;
//Serial.print("Leds1 = "); Serial.println(Leds1);
ItWasDone = true;
LedTime_now = millis();
}
if (ItWasDone && ( millis() - LedTime_now >= LedPeriod) )
{
Leds1 = power_mW_A / 120;
Leds1 = (((2 * Leds1) + 5) / 10) * 5;
// Leds2 = power_mW_B / 120;
// Leds2 = (((2 * Leds2) + 5) / 10) * 5;
//EEPROM.write(0, Leds1);
//EEPROM.write(1, Leds2);
//EEPROM.commit();
//Serial.print("Leds1 = "); Serial.println(Leds1);
ItWasDone = false;
}
*/
if (button.isPressed()) {
Leds1 = 405;
Serial.println("405");
ItWasDone = true;
LedTime_now = millis();
LedTime_now2 = millis();
LedTime_now3 = millis();
LedTime_now4 = millis();
LedTime_now5 = millis();
}
if (ItWasDone && ( millis() - LedTime_now >= LedPeriod) )
{
if (power_mW_A > 300) {
Leds1 = 410;
ItWasDone = false;
ItWasDone2 = true;
} else {
Leds1 = 0;
}
if (ItWasDone2 && ( millis() - LedTime_now2 >= LedPeriod2) )
{
if (power_mW_A > 300) {
Leds1 = 415;
ItWasDone2 = false;
ItWasDone3 = true;
} else {
Leds1 = 5;
}
}
if (ItWasDone3 && ( millis() - LedTime_now3 >= LedPeriod3) )
{
if (power_mW_A > 300) {
Leds1 = 420;
ItWasDone3 = false;
ItWasDone4 = true;
} else {
Leds1 = 10;
}
}
if (ItWasDone4 && ( millis() - LedTime_now4 >= LedPeriod4) )
{
if (power_mW_A > 300) {
Leds1 = 425;
ItWasDone4 = false;
ItWasDone5 = true;
} else {
Leds1 = 15;
}
}
if (ItWasDone5 && ( millis() - LedTime_now5 >= LedPeriod5) )
{
if (power_mW_A > 300) {
Leds1a = 25;
} else {
Leds1 = 20;
}
ItWasDone5 = false;
}
}
if (digitalRead(19) == HIGH) { // SENSOR BOARD TRIGGER
//************ ANALOG INPUT START ****************
Colour1A = (analogRead(36)) * 16;
freq[0] = (Colour1A & 0xFF);
freq[1] = ((Colour1A >> 8) & 0xFF);
Colour1B = (analogRead(39)) * 16;
freq[2] = (Colour1B & 0xFF);
freq[3] = ((Colour1B >> 8) & 0xFF);
Colour1C = (analogRead(34)) * 16;
freq[4] = (Colour1C & 0xFF);
freq[5] = ((Colour1C >> 8) & 0xFF);
Colour1D = (analogRead(35)) * 16;
freq[6] = (Colour1D & 0xFF);
freq[7] = ((Colour1D >> 8) & 0xFF);
Colour2A = (analogRead(32)) * 16;
freq[8] = (Colour2A & 0xFF);
freq[9] = ((Colour2A >> 8) & 0xFF);
Colour2B = (analogRead(33)) * 16;
freq[10] = (Colour2B & 0xFF);
freq[11] = ((Colour2B >> 8) & 0xFF);
Colour2C = (analogRead(25)) * 16;
freq[12] = (Colour2C & 0xFF);
freq[13] = ((Colour2C >> 8) & 0xFF);
Colour2D = (analogRead(26)) * 16;
freq[14] = (Colour2D & 0xFF);
freq[15] = ((Colour2D >> 8) & 0xFF);
//*************** ANALOG INPUT END *****************
} else {
SensorBoard();
}
uint16_t Pot1A = smoothedReading[0] * 16;
light[0] = (Pot1A & 0xFF);
light[1] = ((Pot1A >> 8) & 0xFF);
uint16_t Pot1B = smoothedReading[1] * 16;
analog[0] = (Pot1B & 0xFF);
analog[1] = ((Pot1B >> 8) & 0xFF);
uint16_t Pot2A = smoothedReading[2] * 16;
analog[2] = (Pot2A & 0xFF);
analog[3] = ((Pot2A >> 8) & 0xFF);
uint16_t Pot2B = smoothedReading[3] * 16;
analog[4] = (Pot2B & 0xFF);
analog[5] = ((Pot2B >> 8) & 0xFF);
analog[6] = (Leds1 & 0xFF); // lower byte ANALOG 3 LED OUTPUT 1 VALUE
analog[7] = ((Leds1 >> 8) & 0xFF); // upper byte
analog[8] = (Leds2 & 0xFF); // lower byte ANALOG 4 LED OUTPUT 2 VALUE
analog[9] = ((Leds2 >> 8) & 0xFF); // upper byte
if ((unsigned long)(millis() - SerialTime_now) > SerialPeriod) {
SerialTime_now = millis();
Serial1.write(header, sizeof(header));
Serial1.write(freq, sizeof(freq));
Serial1.write(audio, sizeof(audio));
Serial1.write(acc, sizeof(acc));
Serial1.write(light, sizeof(light));
Serial1.write(analog, sizeof(analog));
Serial1.write(end, sizeof(end));
}
if ((unsigned long)(millis() - inaTime_now) > inaPeriod) {
inaTime_now = millis();
Serial.print("Shunt Voltage_A: "); Serial.print(shuntvoltage_A); Serial.println(" mV");
Serial.print("Bus Voltage_A: "); Serial.print(busvoltage_A); Serial.println(" V");
Serial.print("Load Voltage_A: "); Serial.print(loadvoltage_A); Serial.println(" V");
Serial.print("Current_A: "); Serial.print(current_mA_A); Serial.println(" mA");
Serial.print("Power_A: "); Serial.print(power_mW_A); Serial.println(" mW");
Serial.println("");
Serial.print("Shunt Voltage_B: "); Serial.print(shuntvoltage_B); Serial.println(" mV");
Serial.print("Bus Voltage_B: "); Serial.print(busvoltage_B); Serial.println(" V");
Serial.print("Load Voltage_B: "); Serial.print(loadvoltage_B); Serial.println(" V");
Serial.print("Current_B: "); Serial.print(current_mA_B); Serial.println(" mA");
Serial.print("Power_B: "); Serial.print(power_mW_B); Serial.println(" mW");
Serial.println("");
}
}
void SensorBoard() // SENSOR BOARD READ ROUTINE
{
while (Serial1.available() > 0) // Something to receive?
{
receivedBytes[idx++] = Serial1.read(); // Read next byte into array
if (! started) // Are we still trying to find the start marker?
{
if (receivedBytes[idx - 1] != startMarker[idx - 1]) // Check if the byte received matches expected.
idx = 0; // If not, start over.
if (idx == START_BYTES) // Have we got all 6 bytes?
started = true; // Then we're started, so will read reset of data.
}
else // We're started.
{
if (idx == LENGTH) // Read to the end?
processData = true; // Set the flag so the data gets processed.
}
}
if (processData) // Flag set?
{
for (uint8_t x = 0; x < LENGTH; x++) // Loop through the array.
{
freq[0] = receivedBytes[6];
freq[1] = receivedBytes[7];
freq[2] = receivedBytes[8];
freq[3] = receivedBytes[9];
freq[4] = receivedBytes[10];
freq[5] = receivedBytes[11];
freq[6] = receivedBytes[12];
freq[7] = receivedBytes[13];
freq[8] = receivedBytes[14];
freq[9] = receivedBytes[15];
freq[10] = receivedBytes[16];
freq[11] = receivedBytes[17];
freq[12] = receivedBytes[18];
freq[13] = receivedBytes[19];
freq[14] = receivedBytes[20];
freq[15] = receivedBytes[21];
freq[16] = receivedBytes[22];
freq[17] = receivedBytes[23];
freq[18] = receivedBytes[24];
freq[19] = receivedBytes[25];
freq[20] = receivedBytes[26];
freq[21] = receivedBytes[27];
freq[22] = receivedBytes[28];
freq[23] = receivedBytes[29];
freq[24] = receivedBytes[30];
freq[25] = receivedBytes[31];
freq[26] = receivedBytes[32];
freq[27] = receivedBytes[33];
freq[28] = receivedBytes[34];
freq[29] = receivedBytes[35];
freq[30] = receivedBytes[36];
freq[31] = receivedBytes[37];
freq[32] = receivedBytes[38];
freq[33] = receivedBytes[39];
freq[34] = receivedBytes[40];
freq[35] = receivedBytes[41];
freq[36] = receivedBytes[42];
freq[37] = receivedBytes[43];
freq[38] = receivedBytes[44];
freq[39] = receivedBytes[45];
freq[40] = receivedBytes[46];
freq[41] = receivedBytes[47];
freq[42] = receivedBytes[48];
freq[43] = receivedBytes[49];
freq[44] = receivedBytes[50];
freq[45] = receivedBytes[51];
freq[46] = receivedBytes[52];
freq[47] = receivedBytes[53];
freq[48] = receivedBytes[54];
freq[49] = receivedBytes[55];
freq[50] = receivedBytes[56];
freq[51] = receivedBytes[57];
freq[52] = receivedBytes[58];
freq[53] = receivedBytes[59];
freq[54] = receivedBytes[60];
freq[55] = receivedBytes[61];
freq[56] = receivedBytes[62];
freq[57] = receivedBytes[63];
freq[58] = receivedBytes[64];
freq[59] = receivedBytes[65];
freq[60] = receivedBytes[66];
freq[61] = receivedBytes[67];
freq[62] = receivedBytes[68];
freq[63] = receivedBytes[69];
audio[0] = receivedBytes[70];
audio[1] = receivedBytes[71];
audio[2] = receivedBytes[72];
audio[3] = receivedBytes[73];
audio[4] = receivedBytes[74];
audio[5] = receivedBytes[75];
acc[0] = receivedBytes[76];
acc[1] = receivedBytes[77];
acc[2] = receivedBytes[78];
acc[3] = receivedBytes[79];
acc[4] = receivedBytes[80];
acc[5] = receivedBytes[81];
}
started = false; // Start looking for the start marker again.
processData = false; // Clear the processing flag.
idx = 0; // Reset the array index to start over.
}
}
void alarm() {
Leds1 = 2000;
}
void shutDown() {
digitalWrite(18, HIGH);
}
[/code]
I tried using only one milli() and have it start again at the end of every if statement, but it wouldn't start over. I'll have to go though it again. At the moment this part of the code is the same as the one I posted in the beginning that obviously doesn't work.