// For Discord channel
// This Wokwi project: https://wokwi.com/projects/405344188326256641
// Serial1 for Wokwi, Serial for real life
#define __SERIAL Serial1
#define NUM_SAMPLES 100
// The leftIndex and rightIndex point to the first free element.
int leftIndex = 0;
int rightIndex = 0;
// Always use "unsigned long" when dealing with millis().
unsigned long leftList[NUM_SAMPLES];
unsigned long rightList[NUM_SAMPLES];
// Variables for the millis timer.
unsigned long previousMillis;
const unsigned long interval = 5000;
void setup()
{
__SERIAL.begin(115200);
__SERIAL.println("Hello, Raspberry Pi Pico!");
// Initialize the indexes.
leftIndex = 0;
rightIndex = 0;
// Give Right a head start
addRight(millis());
}
void loop()
{
unsigned long currentMillis = millis();
// When this is run the first time, millis might still be zero
addRight(millis());
addLeft(millis());
// -----------------------------------------------
// millis timer
// -----------------------------------------------
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
printIt();
addLeft(millis()); // extra sample to left list
}
delay(1000); // slow down the sketch
}
void addLeft(unsigned long sample)
{
if(leftIndex >= NUM_SAMPLES)
{
__SERIAL.println("Left is full");
}
else
{
__SERIAL.print("Add Left, ");
leftList[leftIndex++] = sample;
}
}
void addRight(unsigned long sample)
{
if(rightIndex >= NUM_SAMPLES)
{
__SERIAL.println("Right is full");
}
else
{
__SERIAL.print("Add Right, ");
rightList[rightIndex++] = sample;
}
}
void printIt()
{
__SERIAL.println();
__SERIAL.println("--------------------------------");
__SERIAL.print("leftIndex = ");
__SERIAL.print(leftIndex);
__SERIAL.print(", rightIndex = ");
__SERIAL.print(rightIndex);
__SERIAL.println();
__SERIAL.print("(L,R)=");
int both = min(leftIndex, rightIndex);
for(int i=0; i<both; i++)
{
__SERIAL.print("(");
__SERIAL.print(leftList[i]);
__SERIAL.print(",");
__SERIAL.print(rightList[i]);
__SERIAL.print("),");
}
if(leftIndex == rightIndex)
{
__SERIAL.println();
__SERIAL.print("There is no remaining data.");
}
else if(leftIndex > rightIndex)
{
__SERIAL.println();
__SERIAL.print("RemainingLeft=");
for(int i=both; i<leftIndex; i++)
{
__SERIAL.print(leftList[i]);
__SERIAL.print(",");
}
}
else // the rightIndex is higher than the leftIndex
{
__SERIAL.println();
__SERIAL.print("RemainingRight=");
for(int i=both; i<rightIndex; i++)
{
__SERIAL.print(rightList[i]);
__SERIAL.print(",");
}
}
__SERIAL.println();
__SERIAL.println("--------------------------------");
}