Steefmaester

Reception trame Arduino

Feb 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. //by steef
  2. const float seuil = 3.4;// voltage seuil en sorti de la photodiode
  3. const int thisPin = 0;//pin sur laquel est branché la led
  4. const int duree_bit = 100 ;//duré d'un bit (NRZ) en millisecondes
  5. const int TAILLETRAME = 9*8; //nb bit dans la trame
  6. unsigned char TRAME[TAILLETRAME];
  7.  
  8. bool ValBit(int thisPin);
  9. unsigned char bintochar(unsigned char* TRAME, int debut);
  10.  
  11. void setup() {
  12.   pinMode(13, OUTPUT);
  13.   Serial.begin(9600);
  14. }
  15.  
  16.  
  17. void loop() {
  18.  
  19.   if (ValBit(thisPin)== false){
  20.     Serial.println("DEBUT");
  21.   delay(duree_bit);
  22.     for(int i =0; i< TAILLETRAME; i++)
  23.     {
  24.     if (ValBit(thisPin)== false)
  25.       {
  26.       TRAME[i]=0;
  27.       //Serial.println("0");
  28.        analogWrite(13, 0);
  29.     }
  30.     else
  31.     {
  32.       TRAME[i]=1;
  33.       //Serial.println("1");
  34.        analogWrite(13, 255);
  35.     }
  36.       delay(duree_bit);
  37.     }
  38.   int j =0;
  39.   for(int i=0;i<TAILLETRAME/8;i++)
  40.   {
  41.     Serial.println(char(bintochar(TRAME,j)));
  42.     j=j+8;
  43.   }
  44.   }
  45. }
  46.  
  47. bool ValBit(int thisPin){
  48.  int diode = analogRead(thisPin);
  49.   float voltage = diode * (5.0 / 1023.0);
  50.   if(voltage <= seuil)
  51.    return false;
  52.   else
  53.     return true;
  54. }
  55. unsigned char bintochar(unsigned char* TRAME, int debut){
  56.   unsigned char val=0;
  57.   int poid =128;
  58.   for(int i=debut;i<debut+8;i++)
  59.   {
  60.     //Serial.println(int(TRAME[i]));
  61.     val=val+(int(TRAME[i])*poid);
  62.     poid = poid/2;
  63.   }
  64.   return val;
  65. }
Add Comment
Please, Sign In to add comment