Binary data
////////////////////////////////////////
//Lettura e stampa di dati binari (hex + binary)
String str;
void setup()
{
Serial.begin(9600);
for(int i=2; i <= 13; i++)
{
pinMode(i, INPUT);
digitalWrite(i, HIGH); // turn on pull-ups (Attenzione al Pin 13!!!!!)
}
}
void loop()
{
if( Serial.available()){//Aspetta un tasto qualsiasi
str = Serial.readString();
int values = 0;
int bit = 0;
for(int i=2; i <= 13; i++)
{
bitWrite(values, bit, digitalRead(i));
bit = bit + 1;
}
sendBin(values); // send the integer
delay(1000); //send every second
}
}
void sendBin( int value)
{
Serial.print(highByte(value),HEX);
Serial.print(lowByte(value),HEX);
Serial.print(" ");
print_binary(value,12);
Serial.print(" ");
}
void print_binary(int n,int nb)
{
int k;
for (int c = nb-1; c >= 0; c--)
{
k = n >> c;
if (k & 1)
Serial.print("1");
else
Serial.print("0");
}
}