Étape 6: Partie 2: codage
CODE DE L’ÉMETTEUR
------------------------------------------------------------------------
#include < VirtualWire.h >
const int led_pin = 13 ;
const int transmit_pin = 9 ;
const int sensor_pin = 7 ;
int sensor_value ;
void setup() {}
vw_set_tx_pin(transmit_pin) ;
vw_setup(2000) ;
pinMode (led_pin, sortie) ;
pinMode(sensor_pin,INPUT) ;
}
void loop() {}
sensor_value = digitalRead(sensor_pin) ;
msg de char [3] = {« o », « f », « f »} ;
Si (sensor_value == 1) {}
msg [0] = ' o ' ;
msg [1] = ' n ' ;
msg [2] = « # » ;
}
digitalWrite (led_pin, HIGH) ;
vw_send ((uint8_t *) msg, 3) ;
vw_wait_tx() ;
digitalWrite (led_pin, basse) ;
Delay(1000) ;
}
-----------------------------------------------------------------------------
CODE DU RÉCEPTEUR
----------------------------------------------------------------------------
#include < VirtualWire.h >
const int motion_pin = 10 ;
const int led_pin = 13 ;
const int transmit_pin = 9 ;
const int receive_pin = 6 ;
void setup() {}
Delay(1000) ;
Serial.Begin(9600) ;
Serial.println("Setup") ;
vw_set_rx_pin(receive_pin) ;
vw_setup(2000) ;
vw_rx_start() ;
pinMode (led_pin, sortie) ;
pinMode (motion_pin, sortie) ;
}
void loop() {}
uint8_t buf [VW_MAX_MESSAGE_LEN] ;
uint8_t buflen = VW_MAX_MESSAGE_LEN ;
Si (vw_get_message (buf, & buflen)) {}
digitalWrite (led_pin, HIGH) ;
Serial.Print ("a:") ;
pour (int i = 0; i < buflen; i ++) {}
Serial.Print(Char(BUF[i])) ;
Serial.Print(' ') ;
}
{if(Char(BUF[2])=='#')}
digitalWrite (motion_pin, HIGH) ;
Delay(1000) ;
digitalWrite (motion_pin, basse) ;
}
Serial.println() ;
digitalWrite (led_pin, basse) ;
}
}
-------------------------------------------------------------