Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //sender
- #include <VirtualWire.h> //library for simple wireless communication
- const int triggerPin = 2; //defining which input pins do what
- const int clipPin = 4;
- int triggerState = 0; //the variables holding the state of the switches
- int clipState = 0;
- int lastTriggerState=0; //these variables are used to determine if the
- int lastClipState=0; //switch state has changed since the last check
- void setup()
- {
- vw_setup(2000); // bits per sec, specific to transmitter
- vw_set_tx_pin(12); //transmitter pin
- pinMode(triggerPin,INPUT); //defining input pins
- pinMode(clipPin,INPUT);
- Serial.begin(9600); //for testing
- }
- void loop()
- {
- char str[8]; //the string that will be sent wirelessly
- triggerState = digitalRead(triggerPin); //check the trigger pin
- if(triggerState == HIGH)
- {
- strcpy (str,"1"); //if it's pressed, we send a "1"
- }
- else
- {
- strcpy (str,"0"); //otherwise, send a "0"
- }
- strcat (str," "); //this adds a space between the two sent values
- clipState = digitalRead(clipPin); //check the magazine pin
- if(clipState == HIGH)
- {
- strcat (str,"1"); //if it's pressed, send "1", like before
- }
- else
- {
- strcat (str,"0");
- }
- if(lastTriggerState!=triggerState||lastClipState!=clipState)
- { //this makes sure we only send data if it's not the exact
- send(str); //same as it was before, that would be redundant.
- } //send is a defined function below
- lastTriggerState=triggerState;
- lastClipState=clipState;
- }
- void send (char *message)
- {
- Serial.println(message); //print to the serial port, for testing
- vw_send((uint8_t *)message, strlen(message)); //send the string out
- vw_wait_tx(); // wait until the whole message is gone
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement