Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Final Project - Transmitter
#include <RFM69.h>
#include <SPI.h> // the RFM69 library uses SPI
RFM69 radio;
#define myFrequency RF69_915MHZ // or RF69_433MHZ (check your radio)
int myNetwork = 147; // radios must share the same network (0-255)
int myID = 1; // radios should be given unique ID's (0-254, 255 = BROADCAST)
int hubID = 0; // the receiver for all sensor nodes in this example
// instead of sending a string, we can send a struct
// this struct must be shared between all nodes
typedef struct {
int sensor0;
int sensor1;
//int sensor2;
//int sensor3;
} Packet;
int ledPin = 5;
///////////////////////////
///////////////////////////
///////////////////////////
void setup() {
// setup the radio
radio.initialize(myFrequency, myID, myNetwork);
// this example only uses Serial inside setup()
// because Narcoleptic will stop Serial once used
Serial.begin(9600);
Serial.println("\nRADIO INITIALIZED");
Serial.println("Sending sensor values");
pinMode(ledPin, OUTPUT);
pinMode(6, INPUT);
}
///////////////////////////
///////////////////////////
///////////////////////////
void loop() {
delay(50);
// create new instance of our Packet struct
Packet packet;
packet.sensor0 = digitalRead(6); // read values from the analog pins
packet.sensor1 = analogRead(A0);
int numberOfRetries = 5;
// send reliable packet to the hub
// notice the & next to packet when sending a struct
boolean gotACK = radio.sendWithRetry(hubID, &packet, sizeof(packet), numberOfRetries);
if (gotACK) {
Serial.println("got acknowledgment");
}
else {
Serial.println("failed acknowledgment");
}
int heartState = digitalRead(6);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (heartState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment