Advertisement
Guest User

Arduino Gun Transmitter Sketch

a guest
Jun 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. //sender
  2.  
  3. #include <VirtualWire.h> //library for simple wireless communication
  4.  
  5. const int triggerPin = 2; //defining which input pins do what
  6. const int clipPin = 4;
  7.  
  8. int triggerState = 0; //the variables holding the state of the switches
  9. int clipState = 0;
  10.  
  11. int lastTriggerState=0; //these variables are used to determine if the
  12. int lastClipState=0; //switch state has changed since the last check
  13.  
  14. void setup()
  15. {
  16. vw_setup(2000); // bits per sec, specific to transmitter
  17. vw_set_tx_pin(12); //transmitter pin
  18. pinMode(triggerPin,INPUT); //defining input pins
  19. pinMode(clipPin,INPUT);
  20. Serial.begin(9600); //for testing
  21. }
  22.  
  23. void loop()
  24. {
  25. char str[8]; //the string that will be sent wirelessly
  26. triggerState = digitalRead(triggerPin); //check the trigger pin
  27. if(triggerState == HIGH)
  28. {
  29. strcpy (str,"1"); //if it's pressed, we send a "1"
  30. }
  31. else
  32. {
  33. strcpy (str,"0"); //otherwise, send a "0"
  34. }
  35. strcat (str," "); //this adds a space between the two sent values
  36. clipState = digitalRead(clipPin); //check the magazine pin
  37. if(clipState == HIGH)
  38. {
  39. strcat (str,"1"); //if it's pressed, send "1", like before
  40. }
  41. else
  42. {
  43. strcat (str,"0");
  44. }
  45. if(lastTriggerState!=triggerState||lastClipState!=clipState)
  46. { //this makes sure we only send data if it's not the exact
  47. send(str); //same as it was before, that would be redundant.
  48. } //send is a defined function below
  49. lastTriggerState=triggerState;
  50. lastClipState=clipState;
  51. }
  52.  
  53. void send (char *message)
  54. {
  55. Serial.println(message); //print to the serial port, for testing
  56. vw_send((uint8_t *)message, strlen(message)); //send the string out
  57. vw_wait_tx(); // wait until the whole message is gone
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement