#include <Arduino.h>
// upstream:
// https://wokwi.com/projects/349147105137263187
// this:
// https://wokwi.com/projects/349150881872085587
byte pins[] = {A0, A2};
int out[] = {254, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 2};
int _in[] = { 9, 97, 105, 113, 124, 134, 147, 164, 185, 218, 255, 317, 408, 1014};
struct Thresholds {
int black;
int white;
int grey;
};
Thresholds threholds;
void setup() {
Serial.begin(115200);
Serial.println("n");
threholds = {
.black = 505,
.white = 666,
.grey = 580,
};
}
// note: the _in array should have increasing values
int multiMap(int val, int* _in, int* _out, uint8_t size) {
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
// Serial.print(" DEBUG: ");
Serial.println(val);
if (val <= _in[0]) return _out[0];
// Serial.print(" SIEVE ");
if (val >= _in[size - 1]) return _out[size - 1];
// nothing seen; above is a 100 percent sieve. ;)
// Serial.print(" DEBUG: ");
Serial.println(val);
if (val >= _in[size - 1]) return _out[size - 1];
// search right interval
uint8_t pos = 1; // _in[0] allready tested
while (val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return (val - _in[pos - 1]) * (_out[pos] - _out[pos - 1]) / (_in[pos] - _in[pos - 1]) + _out[pos - 1];
}
void getValue(byte pin) {
int value = analogRead(pin);
int mapped_Value = multiMap(value, _in, out, 14);
Serial.print("unmapped: ");
Serial.print(value);
Serial.print(" mapped: ");
Serial.println(mapped_Value);
}
void loop() {
//Serial.println(threholds.black);
getValue(pins[0]);
delay(10); // not needed speeds up simulation
}
// end