safwan092

TX-code

Apr 1st, 2021
786
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <RH_ASK.h>
  2. #include <SPI.h> // Not actually used but needed to compile
  3.  
  4. // defines pins numbers
  5. //transmitter needs Pin 12
  6. #define button1   9
  7. #define button2   10
  8.  
  9. int state1;
  10. int state2;
  11. const char *msg = "0";
  12. RH_ASK driver;
  13.  
  14.  
  15. void setup()
  16. {
  17.   Serial.begin(9600);
  18.   pinMode(button1, INPUT);
  19.   pinMode(button2, INPUT);
  20.   if (!driver.init())Serial.println("init failed");
  21. }
  22.  
  23. void loop(){
  24.   state1 = digitalRead(button1);
  25.   state2 = digitalRead(button2);
  26.  
  27.   if (state1 == 1) {
  28.     send1();
  29.   }
  30.   else if (state2 == 1) {
  31.     send2();
  32.   }
  33.   else {
  34.     send0();
  35.   }
  36.  
  37. }// end of Loop
  38.  
  39.  
  40.  
  41. void send0() {
  42.   msg = "0";
  43.   driver.send((uint8_t *)msg, strlen(msg));
  44.   driver.waitPacketSent();
  45.   delay(200);
  46. }
  47.  
  48. void send1() {
  49.   msg = "1";
  50.   driver.send((uint8_t *)msg, strlen(msg));
  51.   driver.waitPacketSent();
  52.   delay(200);
  53. }
  54.  
  55. void send2() {
  56.   msg = "2";
  57.   driver.send((uint8_t *)msg, strlen(msg));
  58.   driver.waitPacketSent();
  59.   delay(200);
  60. }
  61.  
RAW Paste Data