Come impostare un opzione con bool

ciao a tutti, vorrei chiedere una cosa forse semplice, ma non so come impostarla

in sostanza, vorrei fare un bool che sia vero o falso con 1 o 0, ma al posto di ascoltare come esempio qui sotto una porta digitale, un if di un valore variabile, che nel caso if va in falso, risulta 0

bool esempio = digitalRead(2);

come posso fare, al posto di sentire il valore che ce sulla poarte digitale, posso fare con un if?

L'hai appena scritto, ma in modo sbagliato.


if (digitalRead(2) == LOW) {
  esempio = false;
}
else {
  esempio = true;
}

al contrario se vuoi che debba leggere il valore alto (tasto non premuto, almeno si presuppone) al posto di LOW metti HIGH.
Ovviamente devi dichiarare la variabile bool esempio.

bool esempio;

puoi assegnare il risultato di un'espressione condizionale a un booleano

bool esempio = (digitalRead(2) == HIGH); // true se HIGH

int x = 8;
bool esempio = (x == 7); // false

ovviamente con un if

int x = 8;
bool esempio;

if (x == 7) {
  esempio = true ;
} else { 
  esempio = false ;
}

o con l'operatore ternario

int x = 8;
bool esempio;

esempio = (x == 7) ? true : false;

Beh, in effetti rileggendo la domanda, la tua risposta è più appropriata. :+1:
anzi, più esaustiva!

ottimo grazie ora mi è più chiaro

Comunque bastava un

bool esempio = digitalRead(2);

Oppure se serve "negato"

bool esempio = !digitalRead(2);

un ultima cosa

#define pin_data 3
#define bit_per_sec 1
int piezoPin = 8;
int f1 = 201;
int f2 = 301;
int f1_ = 0;
void setup() {
  Serial.begin(9600);
  pinMode(pin_data, OUTPUT);
  //tone(piezoPin, 190);
  //delay(200);
  //pinMode(piezoPin, INPUT);
  //delay(200);
 // pinMode(piezoPin, OUTPUT);
  tone(piezoPin, 190);
  delay(2000);
  //pinMode(piezoPin, INPUT);
 // delay(200);
  trasmitt_bit();
}
void trasmitt_bit()
{
  const char *message = "?";
  for (int byte_idx = 0; byte_idx < strlen(message); byte_idx++) {
    char tx_byte = message[byte_idx];
    for (int bit_idx = 0; bit_idx < 8; bit_idx++) {
      bool tx_bit = tx_byte & (0x80 >> bit_idx);
      digitalWrite(pin_data, tx_bit);
      if (tx_bit == 0)
      {
        {
          pinMode(pin_data, OUTPUT);
          tone(piezoPin, f1);
          f2 = 301;
          f1_ = 0;
        }
      }
      if (tx_bit == 1)
      {
        {
          f1_ = f1+1;
          Serial.println(f2);

          pinMode(pin_data, OUTPUT);
          tone(piezoPin, f2);
        }

        if (f1_==1)
        {
          f2=f2+20;
          
        }
        if (f1_==2)
        {
          f2=f2+20;
          
        }
        if (f1_==3)
        {
          f2=f2+20;
          
        }
        if (f1_==4)
        {
          f2=f2+20;
          
        }
        if (f1_==5)
        {
          f2=f2+20;
          
        }
        if (f1_==6)
        {
          f2=f2+20;
          
        }
        if (f1_==7)
        {
          f2=f2+20;
          
        }
        
      }
       Serial.print(f2);

      delay(1000 / bit_per_sec);
    }
  }
  pinMode(pin_data, OUTPUT);
  tone(piezoPin, 290);
  delay(200);
  pinMode(piezoPin, INPUT);
  delay(200);
  pinMode(piezoPin, OUTPUT);
  tone(piezoPin, 290);
  delay(200);
  pinMode(piezoPin, INPUT);
  delay(200);
  
  pinMode(piezoPin, INPUT);
}
void loop() {
}

su:

 if (tx_bit == 1)
      {
        {
          f1_ = f1+1;
          Serial.println(f2);

          pinMode(pin_data, OUTPUT);
          tone(piezoPin, f2);
        }

come posso fare che non venga eseguito in paralelo ma separataemente incrementando "f1_"?

niente scusate, ho trovato errore ho dimenticato di togliere un Serial.print ed ho scritto ```
f1_ = f1+1;

ma è 

          f1_ = f1_+1;

Sei sicuro di questo blocco di codice?
Tutta quella sfilza di istruzione è equivalente a questo:

  if ((f1_ > 0) && (f1_ < 8 )) 
  {
     f2 += 20;
  }

an si giusto una semplificazione, è la stessa cosa, non ci avevo pensato, grazie

o un po' più ottimizzato

if (f1 & 0b111) f2 += 20; // se almeno uno dei primi 3 bit è 1 (valore da 1 a 7) allora...

:slight_smile:

alla fine ho fatto cosi:

#define pin_data 3
#define bit_per_sec 5
int piezoPin = 8;
int f1 = 201;
int f2 = 301;
int f1_ = 0;
int f2_ = 0;
void setup() {
  Serial.begin(9600);
  pinMode(pin_data, OUTPUT);
  //tone(piezoPin, 190);
  //delay(200);
  //pinMode(piezoPin, INPUT);
  //delay(200);
 // pinMode(piezoPin, OUTPUT);
  tone(piezoPin, 190);
  delay(2000);
  //pinMode(piezoPin, INPUT);
 // delay(200);
  trasmitt_bit();
}
void trasmitt_bit()
{
  const char *message = "r";
  for (int byte_idx = 0; byte_idx < strlen(message); byte_idx++) {
    char tx_byte = message[byte_idx];
    for (int bit_idx = 0; bit_idx < 8; bit_idx++) {
      bool tx_bit = tx_byte & (0x80 >> bit_idx);
      digitalWrite(pin_data, tx_bit);
      if (tx_bit == 0)
      {
        {
          pinMode(pin_data, OUTPUT);
          tone(piezoPin, f1);
          f2_= f2_ +1;
          f2 = 301;
          f1_ = 0;
          
          Serial.println(f2_);

        }
      }

      if ((f2_ > 0) && (f2_ < 8 )) 
  {
     f1 += 20;
  }
      if (tx_bit == 1)
      {
        {
          f1_ = f1_+1;
          f2_ = 0;
          f1 = 201;
          Serial.println(f1_);

          pinMode(pin_data, OUTPUT);
          tone(piezoPin, f2);
        }
if ((f1_ > 0) && (f1_ < 8 )) 
  {
     f2 += 20;
  }
  
        
      }

      delay(1000 / bit_per_sec);
    }
  }
  pinMode(pin_data, OUTPUT);
  tone(piezoPin, 290);
  delay(200);
  pinMode(piezoPin, INPUT);
  delay(200);
  pinMode(piezoPin, OUTPUT);
  tone(piezoPin, 290);
  delay(200);
  pinMode(piezoPin, INPUT);
  delay(200);
  
  pinMode(piezoPin, INPUT);
}
void loop() {
}

c’è un modo di semplificare il messaggio? secondo voi, nel senso, trasmettere meno bit possibili

Trasmettere meno bit significa comprimere il messaggio:
https://it.wikipedia.org/wiki/Codifica_di_Huffman

Il primo esempio di codice compresso:
https://it.wikipedia.org/wiki/Codice_Morse

eh si, ma su arduino e C? in base a quello che ho già scritto?
come?
faro base64

Base64 allunga i messaggi del 33%, non è un codice di compressione :wink:

si perchè lo mette in ascii giusto?

char message [16];
byte rx_byte = 0;
bool rx_bit = 0;
int bit_position = 0;
#include "arduinoFFT.h"
//int on_ric = 0;
int ok = 0;
int inizio = 0;
//int on_ric_ok = 0;
//int on = 0;
int ok_ = 0;
#define SAMPLES 128
#define SAMPLING_FREQUENCY 2000

arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[SAMPLES];
double vImag[SAMPLES];



void setup() {
  Serial.begin(115200);
  sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
  strcpy(message, "");

}
void loop() {

  for (int i = 0; i < SAMPLES; i++) {
    microseconds = micros();
    vReal[i] = analogRead(A1);
    vImag[i] = 0;
    while (micros() < (microseconds + sampling_period_us)) {};
  }
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
  double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);


  for (int i = 0; i < SAMPLES / 2; i++) {
    //Serial.println(vReal[i], 1);
  }

  if ((peak > 153.00f) && (peak < 154.00f ))


  {

    //ok = ok ++;
    ok = 1;
    ok_ = 1;
   // Serial.print("on");
  }

  if ((ok == 1) && (peak > 204.00f) && (peak < 205.00f ))

  {
    bool rx_bit = 0;
    ok = 2;
    ok_ = 1;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
        //Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
         // Serial.println(message);
         Serial.print("0");

      }
    }
  }



  if ((ok == 2) && (peak > 223.00f) && (peak < 225.00f ))

  {
        bool rx_bit = 0;
    ok_ = 1;
    //rx_bit = 0;
    ok = 3;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
       // Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
         // Serial.println(message);
                     Serial.print("0");

      }


    }
  }

  if ((ok == 3) && (peak > 246.00f) && (peak < 247.00f ))

  {
    //rx_bit = 0;
        bool rx_bit = 0;
         ok_ = 1;
    ok = 4;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
       // Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
         // Serial.println(message);
         Serial.print("0");

      }


    }
  }

  if ((ok == 4) && (peak > 266.00f) && (peak < 267.00f ))

  {
//    rx_bit = 0;
    bool rx_bit = 0;
ok_ = 1;
    ok = 4;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
//        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
  //        Serial.println(message);
         Serial.print("0");

      }


    }
  }

  if ((ok == 4) && (peak > 284.00f) && (peak < 286.00f ))

  {
//    rx_bit = 0;
    bool rx_bit = 0;
 ok_ = 1;

    ok = 5;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
//        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
  //       Serial.println(message);
            Serial.print("0");

      }


    }
  }

  if ((ok == 5) && (peak > 306.00f) && (peak < 307.00f ))

  {
    bool rx_bit = 0;
ok_ = 1;
    //rx_bit = 0;
    ok = 6;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
   //        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
     //   Serial.println(message);
                 Serial.print("0");

      }


    }
  }

  if ((ok == 6) && (peak > 328.00f) && (peak < 329.00f ))

  {
   // rx_bit = 0;
        bool rx_bit = 0;
ok_ = 1;
    ok = 7;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
 //        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
   //       Serial.println(message);
                       Serial.print("0");

      }


    }
  }

  if ((ok == 7) && (peak > 346.00f) && (peak < 347.00f ))

  {
    bool rx_bit = 0;
ok_ = 1;
//    rx_bit == 0;
    ok = 8;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
//        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
  //        Serial.println(message);
         Serial.print("0");

      }


    }
  }

  if ((ok == 8) && (peak > 365.00f) && (peak < 367.00f ))

  {

       bool rx_bit = 0;
ok_ = 1;
//    rx_bit = 0;
    ok = 1;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
//        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
  //        Serial.println(message);
         Serial.print("0");

      }


    }
  }

  if ((ok_ == 1) && (peak > 409.00f) && (peak < 410.00f ))

  {

    bool rx_bit = 1;
    Serial.println("si");

//    rx_bit = 1;
    ok_ = 2;
    ok = 1;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
//        Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
  //        Serial.println(message);
         Serial.print("1");

      
      }


    }


  }



  if ((ok_ == 2) && (peak > 428.00f) && (peak < 429.00f ))

  {

    bool rx_bit = 1;

//    rx_bit = 1;
    ok = 1;
    ok_ = 3;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);
       

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
       // Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
         // Serial.println(message);
         Serial.print("1");

      }


    }
  }

  if ((ok_ == 3) && (peak > 451.00f) && (peak < 452.00f ))

  {

    bool rx_bit = 1;

//    rx_bit == 1;
    ok = 1;
    ok_ = 4;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);
            }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
        //Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
       //   Serial.println(message);
         Serial.print("1");

      }


    }
  }

  if ((ok_ == 4) && (peak > 470.00f) && (peak < 472.00f ))

  {
        bool rx_bit = 1;
    //rx_bit = 1;
    ok = 1;
    ok_ = 5;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);
        

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
        //Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
          //Serial.println(message);
         Serial.print("1");

      
      }


    }
  }

  if ((ok_ == 5) && (peak > 491.00f) && (peak < 492.00f ))

  {
        bool rx_bit = 1;

    //rx_bit = 1;
    ok = 1;
    ok_ = 6;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
       // Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
         // Serial.println(message);
Serial.print("1");
      
      }


    }
  }

  if ((ok_ == 6) && (peak > 500.00f) && (peak < 512.00f ))

  {
       bool rx_bit = 1;

    //rx_bit = 1;
    ok = 1;
    ok_ = 7;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
       // Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
        //  Serial.println(message);

      Serial.print("1");
      }


    }
  }

  if ((ok_ == 7) && (peak > 532.00f) && (peak < 533.20f ))


  {    bool rx_bit = 1;

//    rx_bit = 1;
    ok = 1;
    ok_ = 8;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
        //Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
          //Serial.println(message);

      
      }


    }
  }

  if ((ok_ == 8) && (peak > 550.00f) && (peak < 551.20f ))

  {
        bool rx_bit = 1;
    //rx_bit = 1;
    ok_ = 1;
    ok = 1;
    if (rx_bit)
    {
      rx_byte |= (0x08 >> bit_position);
    }

    bit_position += 1;

    if (bit_position == 8) {

      strncat(message, &rx_byte, 1);

    }

    for (int e = 0; e < 8; e += 1) {
      if (e < bit_position) {
        //Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");
        // Serial.println(message);

        Serial.print("1");

      }


    }
  }

 
  /* if ((ok > 0) &&(peak > 151.00f) && (peak < 152.00f ))


    {

      //ok = ok ++;
      ok = 0;
      Serial.print("off");


    }
  */

//Serial.println(peak);
  delay(5);




}

ho un problema con il decode, i bit si ricevono, pero mettendo un print di 1 o 0 dentro il ciclo for ho notato che mi stampa il valore 1 volta, poi sdoppiato e poi quadruplicato

come posso fare per avere i bit giusti come gli ricevo e convertirli in lettere in tempo reale?

cosa ti aspetti da


ho capito cosa aveva
comunque ti dico il risultato finale che voglio, bisogna che si segna i bit e converte in tempo reale in ascii, se metto il print dentro al ciclo for, quando shifta in la, ripete il bit più volte, quindi credevo che sbagliava

ma teoricamente se metto:

Serial.println(rx_byte & (0x80 >> e) ? "1" : "0");

dovrebbe stamparmi i valori al posto giusto

ma dopo di tutto ciò, una volta che ne ha ricevuti 8 bit, lo traduce? in ascii cosi?