// structure search with auto for loop
// 2nd smallest not working
struct MaintenanceSchedule {
const char *label;
uint16_t presets;
uint16_t countsToGo;
uint16_t leastRemaining;
bool needsMaintenance;
};
MaintenanceSchedule parts[] = {
{ "Pusher", 13, 5, 0, false }, // maintain every 7 cycles
{ "Rocker", 9, 0, 0, false }, // maintain every 10 cycles
{ "Plate", 47, 0, 0, false }, // maintain every 3 cycles
{ "Zerks", 10, 0, 0, false }, // maintain every 2 cycles
{ "Cylinder", 17, 0, 0, false }, // maintain every 2 cycles
};
const byte button1 = A0;
const int NUM_OF_ELEMENTS = sizeof(parts) / sizeof(parts[0]);
int currentCount = 1;
void setup() {
Serial.begin(115200);
pinMode(button1, INPUT_PULLUP);
Serial.println("presets");
for (auto togo : parts) {
if (currentCount <= togo.presets) {
Serial.println(togo.presets);
}
}
}// end of setup
void loop() {
static byte lastPress;
static byte isPressed;
byte pressed = digitalRead(button1);
// one shot code
isPressed = pressed and lastPress;
lastPress=!pressed;
if (isPressed==HIGH) {
small();
currentCount++;
}
//Serial.println(pressed);
}
void small() {
//
// record counts to go for each item
//
for (auto &lowest : parts) { // ampersand needed for write access to struct
if ( currentCount < lowest.presets) {
lowest.countsToGo = lowest.presets - currentCount;
}
else lowest.countsToGo = lowest.presets - currentCount % lowest.presets;
}
Serial.println("\n================\n");
Serial.println("counts to go");
// print counts to go
for (auto togo : parts) {
//if (currentCount <= togo.countsToGo) {
Serial.println(togo.countsToGo);
//}
}
// add code to print data of lowest and second item
int smallest = INT16_MAX;
int secondSmallest = INT16_MAX;
if (parts[0].countsToGo < parts[1].countsToGo) {
smallest = parts[0].countsToGo;
secondSmallest = parts[1].countsToGo;
}
else {
smallest = parts[1].countsToGo;
secondSmallest = parts[0].countsToGo;
}
for (int i = 1; i < 5; i++) {
if (smallest > parts[i].countsToGo) {
secondSmallest = smallest;
smallest = parts[i].countsToGo;
}
else if (parts[i].countsToGo < secondSmallest && parts[i].countsToGo != smallest) {
secondSmallest = parts[i].countsToGo;
}
}
Serial.println("\n================\n");
Serial.print("smallest\t");
Serial.println(smallest);
Serial.print("secondSmallest\t");
Serial.println(secondSmallest);
}