Hello,
I am using a two SAMD21 Pro RF boards to communicate and drive a stepper motor.
I would like to send 2-digit numbers from one to the other and drive the stepper motor based on the values sent. The code to send and receive RF transmissions works well, but I am having trouble storing the 2-digit number for use on the stepper.
How does one take an input from the serial monitor and store it as an integer??
Errors centered around this part of code:
int in_deg = (char*)buf.toInt()
#include <SPI.h>
//Radio Head Library:
#include <RH_RF95.h>
// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 5
#define stepsPerRevolution 200
// We need to provide the RFM95 module's chip select and interrupt pins to the
// rf95 instance below.On the SparkFun ProRF those pins are 12 and 6 respectively.
RH_RF95 rf95(12, 6);
int LED = 4; //Status LED on pin 4
int packetCounter = 0; //Counts the number of packets sent
long timeSinceLastPacket = 0; //Tracks the time stamp of last packet received
// The broadcast frequency is set to 921.2, but the SADM21 ProRf operates
// anywhere in the range of 902-928MHz in the Americas.
// Europe operates in the frequencies 863-870, center frequency at
// 868MHz.This works but it is unknown how well the radio configures to this frequency:
//float frequency = 864.1;
float frequency = 921.2;
void setup()
{
// Declare pins as output:
pinMode(stepPin, OUTPUT); //stepper
pinMode(dirPin, OUTPUT); //stepper
pinMode(LED, OUTPUT);
SerialUSB.begin(9600);
// It may be difficult to read serial messages on startup. The following
// line will wait for serial to be ready before continuing. Comment out if not needed.
while(!SerialUSB);
SerialUSB.println("RFM Server!");
//Initialize the Radio.
if (rf95.init() == false){
SerialUSB.println("Radio Init Failed - Freezing");
while (1);
}
else{
// An LED indicator to let us know radio initialization has completed.
SerialUSB.println("Receiver up!");
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
rf95.setFrequency(frequency);
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
// rf95.setTxPower(14, false);
}
void loop()
{
if (rf95.available()){
// Should be a message for us now
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
int in_deg = (char*)buf.toInt()
int out_deg = in_deg*2;
if (rf95.recv(buf, &len)){
digitalWrite(LED, HIGH); //Turn on status LED
timeSinceLastPacket = millis(); //Timestamp this packet
SerialUSB.print("Got message: ");
SerialUSB.print((char*)buf);
//SerialUSB.print(" RSSI: ");
//SerialUSB.print(rf95.lastRssi(), DEC);
SerialUSB.println();
// Messages received, now driving motor to angle provided
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
//int steps = ((out_deg*10)/360)*stepsPerRevolution ; //this converts degreees input to steps
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
SerialUSB.print("Motor moved ");
SerialUSB.println(out_deg);
// Send a reply
uint8_t toSend[] = "Got Message!";
rf95.send(toSend, sizeof(toSend));
rf95.waitPacketSent();
SerialUSB.println("Sent a reply");
digitalWrite(LED, LOW); //Turn off status LED
}
else
SerialUSB.println("Recieve failed");
}
//Turn off status LED if we haven't received a packet after 1s
if(millis() - timeSinceLastPacket > 1000){
digitalWrite(LED, LOW); //Turn off status LED
timeSinceLastPacket = millis(); //Don't write LED but every 1s
}
}