How do I do Servo code for RFM69 Radio?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

How do I do Servo code for RFM69 Radio?

Post by animefruit »

Can you tell me the code I need to use for servos to be registered rather than button presses for the RadioHead69_RawDemoTXRX_OLED code?

Do I use the same servo code as usual from the Sweep example sketch or to I have to use something else because it is code for the RFM69 Radio?

Here is the code for the RadioHead69_RawDemoTXRX_OLED code:

Code: Select all

    // rf69 demo tx rx oled.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_RF69 class. RH_RF69 class does not provide for addressing or
// reliability, so you should only use RH_RF69  if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf69_server.
// Demonstrates the use of AES encryption, setting the frequency and modem 
// configuration

#include <SPI.h>
#include <RH_RF69.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

/************ OLED Setup ***************/
Adafruit_SSD1306 oled = Adafruit_SSD1306();

#if defined(ESP8266)
  #define BUTTON_A 0
  #define BUTTON_B 16
  #define BUTTON_C 2
  #define LED      0
#elif defined(ESP32)
  #define BUTTON_A 15
  #define BUTTON_B 32
  #define BUTTON_C 14
  #define LED      13
#elif defined(ARDUINO_STM32F2_FEATHER)
  #define BUTTON_A PA15
  #define BUTTON_B PC7
  #define BUTTON_C PC5
  #define LED PB5
#elif defined(TEENSYDUINO)
  #define BUTTON_A 4
  #define BUTTON_B 3
  #define BUTTON_C 8
  #define LED 13
#elif defined(ARDUINO_NRF52832_FEATHER)
  #define BUTTON_A 31
  #define BUTTON_B 30
  #define BUTTON_C 27
  #define LED 17
#else // 32u4, M0, and 328p
  #define BUTTON_A 9
  #define BUTTON_B 6
  #define BUTTON_C 5
  #define LED      13
#endif


/************ Radio Setup ***************/

// Change to 434.0 or other frequency, must match RX's freq!
//#define RF69_FREQ 915.0
#define RF69_FREQ 434.0
#if defined (__AVR_ATmega32U4__) // Feather 32u4 w/Radio
  #define RFM69_CS      8
  #define RFM69_INT     7
  #define RFM69_RST     4
#endif

#if defined(ARDUINO_SAMD_FEATHER_M0) // Feather M0 w/Radio
  #define RFM69_CS      8
  #define RFM69_INT     3
  #define RFM69_RST     4
#endif

#if defined (__AVR_ATmega328P__)  // Feather 328P w/wing
  #define RFM69_INT     3  // 
  #define RFM69_CS      4  //
  #define RFM69_RST     2  // "A"
#endif

#if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) || defined(ARDUINO_NRF52840_FEATHER) || defined(ARDUINO_NRF52840_FEATHER_SENSE)
  #define RFM69_INT     9  // "A"
  #define RFM69_CS      10  // "B"
  #define RFM69_RST     11  // "C"
  #define LED           13

#elif defined(ESP32)    // ESP32 feather w/wing
  #define RFM69_RST     13   // same as LED
  #define RFM69_CS      33   // "B"
  #define RFM69_INT     27   // "A"
#endif

#if defined(ARDUINO_NRF52832_FEATHER)
  /* nRF52832 feather w/wing */
  #define RFM69_RST     7   // "A"
  #define RFM69_CS      11   // "B"
  #define RFM69_INT     31   // "C"
  #define LED           17
#endif

// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);

void setup() 
{
  delay(500);
  Serial.begin(115200);
  //while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer

  // Initialize OLED display
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  oled.display();
  delay(500);
  oled.clearDisplay();
  oled.display();

  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);

  pinMode(LED, OUTPUT);     
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, LOW);

  Serial.println("Feather RFM69 RX/TX Test!");

  // manual reset
  digitalWrite(RFM69_RST, HIGH);
  delay(10);
  digitalWrite(RFM69_RST, LOW);
  delay(10);
  
  if (!rf69.init()) {
    Serial.println("RFM69 radio init failed");
    while (1);
  }
  Serial.println("RFM69 radio init OK!");
  
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
  // No encryption
  if (!rf69.setFrequency(RF69_FREQ)) {
    Serial.println("setFrequency failed");
  }

  // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  // ishighpowermodule flag set like this:
  rf69.setTxPower(14, true);

  // The encryption key has to be the same as the one in the server
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  rf69.setEncryptionKey(key);
  
  pinMode(LED, OUTPUT);

  Serial.print("RFM69 radio @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");

  // OLED text display tests
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(0,0);
  oled.println("RFM69 @ ");
  oled.print((int)RF69_FREQ);
  oled.println(" MHz");
  oled.display();

  delay(500);
}


void loop()
{  if (rf69.waitAvailableTimeout(100)) {
    // Should be a message for us now   
    uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    
    if (! rf69.recv(buf, &len)) {
      Serial.println("Receive failed");
      return;
    }
    digitalWrite(LED, HIGH);
    rf69.printBuffer("Received: ", buf, len);
    buf[len] = 0;
    
    Serial.print("Got: "); Serial.println((char*)buf);
    Serial.print("RSSI: "); Serial.println(rf69.lastRssi(), DEC);

    oled.clearDisplay();
    oled.setCursor(0,0);
    oled.println((char*)buf);
    oled.print("RSSI: "); oled.print(rf69.lastRssi());
    oled.display(); 
    digitalWrite(LED, LOW);
  }

  if (!digitalRead(BUTTON_A) || !digitalRead(BUTTON_B) || !digitalRead(BUTTON_C))
  {
    Serial.println("Button pressed!");
    
    char radiopacket[20] = "Button #";
    if (!digitalRead(BUTTON_A)) radiopacket[8] = 'A';
    if (!digitalRead(BUTTON_B)) radiopacket[8] = 'B';
    if (!digitalRead(BUTTON_C)) radiopacket[8] = 'C';
    radiopacket[9] = 0;

    Serial.print("Sending "); Serial.println(radiopacket);
    rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
    rf69.waitPacketSent();
  }
}   


I also Simplified the RadioHead69_RawDemoTXRX_OLED

I commented any piece of code that wasn't needed for the code to work.

Here is my Simplified code:

Code: Select all

  #include <RH_RF69.h>



// 32u4, M0, and 328p
  #define BUTTON_A 9
  #define BUTTON_B 6
  #define BUTTON_C 5
  #define LED      13
//#endif


/************ Radio Setup ***************/

// Change to 434.0 or other frequency, must match RX's freq!
//#define RF69_FREQ 915.0
#define RF69_FREQ 434.0

#if defined (__AVR_ATmega328P__)  // Feather 328P w/wing
  #define RFM69_INT     3  // 
  #define RFM69_CS      4  //
  #define RFM69_RST     2  // "A"
#endif

// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);

void setup() 
{
  delay(500);
  Serial.begin(115200);
  


  pinMode(LED, OUTPUT);     
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, LOW);

  
  // manual reset
 

  
  if (!rf69.init()) {
    Serial.println("RFM69 radio init failed");
    while (1);
  }    // failed
  
 
  // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  // ishighpowermodule flag set like this:
  rf69.setTxPower(14, true);


  delay(500);
}


void loop()
{  if (rf69.waitAvailableTimeout(100)) {
    // Should be a message for us now   
    uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    
   if (! rf69.recv(buf , & len )) {
      Serial.println("Receive failed");
      return;
    }
    digitalWrite(LED, HIGH);
    rf69.printBuffer("Received: ", buf, len);
    buf[len] = 0;
    
    Serial.print("Got: "); Serial.println((char*) buf);
   
  }

  if (!digitalRead(BUTTON_A) || !digitalRead(BUTTON_B) || !digitalRead(BUTTON_C))
  {
    Serial.println("Button pressed!");
    
    char radiopacket[20] = "Button #";
    if (!digitalRead(BUTTON_A)) radiopacket[8] = 'A';
    if (!digitalRead(BUTTON_B)) radiopacket[8] = 'B';
    if (!digitalRead(BUTTON_C)) radiopacket[8] = 'C';
    radiopacket[9] = 0;

    Serial.print("Sending "); Serial.println(radiopacket);
    rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
    rf69.waitPacketSent();
  }
}
   

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: How do I do Servo code for RFM69 Radio?

Post by bidrohini »

Not exactly what you're looking for. But You can get some ideas from the following links:
https://lowpowerlab.com/forum/projects/ ... ontrol/15/
https://www.researchgate.net/publicatio ... ransceiver

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I do Servo code for RFM69 Radio?

Post by animefruit »

Thanks bidrohini.

I looked at that code you sent me but still don't know how to include it in my code because that is code for controlling a servo with the radio when I just want code for sending a message to the serial monitor through the radio when the servo has moved.

I think would be best to use this code I made.

Here is the code I made:

Code: Select all

  #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
 
 
#define LED 13// FOR LED
 
 
 
Adafruit_LiquidCrystal lcd(0);
 
Servo myservo1;
Servo myservo2;
 
//cant do A4 and A5(they are for lcd)
// Analogs 0 to 5
const int knockSensor = A0;
const int knockSensor1= A1;
const int knockSensor2= A2;
const int knockSensor3= A3;
const int knockSensor4= A6;
const int knockSensor5= A7;
 
 
const int threshold = 0;
int health = 500;
 
// 6 total sensor readings (from 0 to 5)
 
int sensorReading = 0;
int sensorReading1 =0;
int sensorReading2 =0;
int sensorReading3 =0;
int sensorReading4 =0;
int sensorReading5 =0;
 
 
int pos =0;
 
 
 
int deaths=1;// gonna be number of deaths
 
 
 
void setup()
 
{
 // pinMode(LED_BUILTIN, OUTPUT); //LED ON ARDUINO
  pinMode(LED, OUTPUT);//FOR LED
 
  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
 
// sg90
 
  lcd.begin (20 , 4);
  lcd.clear();
 
 
  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);//was 0
 
  myservo2.attach(10);
  myservo2.write(45);//was 179
 
 
 
 
}
 
void loop()
{
  do
  {
 
 
    myservo1.detach();
    myservo2.detach();
 
    sensorReading = analogRead(knockSensor);
   
 
    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);
 
    if (sensorReading > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading;
 
     
     
      Serial.print(sensorReading);
      lcd.print(sensorReading);
 
 
      int sensorValue = analogRead(A0);
 
      Serial.println(sensorValue);
 
 
     
     
 
 
     
      delay(1);
 
    }
 
//duplicate
 
sensorReading1= analogRead(knockSensor1);
 
 if (sensorReading1 > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading1;
 
     
     
      Serial.print(sensorReading1);
      lcd.print(sensorReading1);
 
 
      int sensorValue = analogRead(A1);
 
      Serial.println(sensorValue);
 
 
      delay(1);
 
    }
 
 
 
//duplicate
 
sensorReading2= analogRead(knockSensor2);
 
 if (sensorReading2 > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading2;
 
     
     
      Serial.print(sensorReading2);
      lcd.print(sensorReading2);
 
 
      int sensorValue = analogRead(A2);
 
      Serial.println(sensorValue);
 
 
      delay(1);
 
    }
 
 
 
 
//duplicate
 
sensorReading3= analogRead(knockSensor3);
 
 if (sensorReading3 > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading3;
 
     
     
      Serial.print(sensorReading3);
      lcd.print(sensorReading3);
 
 
      int sensorValue = analogRead(A3);
 
      Serial.println(sensorValue);
 
 
      delay(1);
 
    }
 
 
 
//duplicate
 
 
 
sensorReading4= analogRead(knockSensor4);
 
 if (sensorReading4 > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading4;
 
     
     
      Serial.print(sensorReading4);
      lcd.print(sensorReading4);
 
 
      int sensorValue = analogRead(A6);
 
      Serial.println(sensorValue);
 
 
      delay(1);
 
    }
 
 
 
//duplicate
 
sensorReading5= analogRead(knockSensor5);
 
 if (sensorReading5 > threshold) {
 
      Serial.println("Knock!");
      health = health - sensorReading5;
 
     
     
      Serial.print(sensorReading5);
      lcd.print(sensorReading5);
 
 
      int sensorValue = analogRead(A7);
 
      Serial.println(sensorValue);
 
 
      delay(1);
 
    }
 
 
 
 
 
 
 
  } while (health > 1);
 
 
 
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);//was 2000             // FOR LED IN ARDUINO
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
 
 
  /*
digitalWrite(LED, HIGH);
delay(2000);                 //FOR LED
digitalWrite(LED, HIGH);
delay(1000);
 */
  myservo1.attach(9);
  myservo2.attach(10);
 
  delay(15);
 
 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);    // waits 15 ms for the servo to reach the position
 
 
  }
 
 delay(1000);
 
 
 lcd.setCursor(0, 2);
      Serial.println(deaths); //showind number of deaths on the lcd
      lcd.println(deaths);
 
 
deaths= deaths + 1;// adding deaths
 
 
 
 
 
 
 
  health = 500;
}
 
    
This code I made moves a servo after the sensors have been pressed to much.

But I want the it to send a message to the serial monitor through the radio when the servo has moved.

Can you tell me what code is used to send a message to the serial monitor through the radio when the servo has moved?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”