// Settings
bool lettura[7] = {0,0,0,0,0,0,0};
int A_lettura = 0;
int tempo = 0;
int ul_lettura = 0;
byte BooltoByte(bool Dig[], bool print_ok)
{
byte byte_var = 0;
// This is used for debugging and can be activated by passing print_ok as true
if (print_ok)
{
Serial.print("\n\n The starting array is: ");
for (int i = 0; i < 7; i++)
{
Serial.print("[");
Serial.print(Dig[i], BIN);
Serial.print("]");
}
}
// Conversion:
for (int i = 0; i < 7; i++)
{
if (Dig[i])
{
byte_var |= (1 << (7-i));
if (print_ok)
{
Serial.print("\n");
Serial.print(i);
Serial.print(". ");
Serial.print(byte_var, BIN);
}
}
}
if (print_ok)
{
Serial.print("\n\n The converted ->");
Serial.print(byte_var, BIN);
}
}
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
//pinMode(1, INPUT); //this crate some problems
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
}
void loop() {
// digitalread
tempo = millis();
if ((tempo - ul_lettura) >= 1000)
{
for( int i = 0; i < sizeof(lettura); i++)
{
lettura[i] = digitalRead(i+2);
}
ul_lettura = millis();
//print vector
Serial.print("\n\n Last read (");
Serial.print(tempo,DEC);
Serial.print("):\n");
for( int i = 0; i < sizeof(lettura); i++)
{
Serial.print("[");
Serial.print(lettura[i]);
Serial.print("]");
}
Serial.print("\n Convertito ->");
Serial.print(BooltoByte(lettura,1),BIN);
}
}