I'm trying to send the x and y values of a joystick between two arduino mega 2560 with an nrf24l01. So far I have a transmitter and reciever code but I get wrong values on the reciever.
Transmitter:
#include <SPI.h>
#include <RF24.h>
RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};
typedef struct{
int X;
int Y;
int K;
}
Values;
Values v;
void setup() {
pinMode(10, INPUT);
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}
void loop() {
radio.stopListening();
v.X = analogRead(A0);
v.Y = analogRead(A1);
v.K = digitalRead(10);
Serial.print("X = ");
Serial.println(v.X);
Serial.print("Y = ");
Serial.println(v.Y);
Serial.print("Button status: ");
if (v.K == HIGH)
{
Serial.println("pressed");
}
else
{
Serial.println("not pressed");
}
radio.write(&v,sizeof(v));
delay(500);
}
Reciever:
#include <SPI.h>
#include <RF24.h>
RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};
typedef struct{
int X;
int Y;
int K;
}
Values;
Values v;
void setup() {
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.startListening();
}
void loop() {
radio.startListening();
if ( radio.available() )
{
while(radio.available())
{
radio.read(&v, sizeof(v));
}
}
radio.stopListening();
Serial.println(v.X);
Serial.println(v.Y);
Serial.println(v.K);
delay(1000);
}
I'm using the pin configuration from this website: https://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
Related
If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}
I am trying to send a double/float over SPI from my SAMD21 based board, with chip select on pin A1/A2. I have copied some code from the internet, but I don't really understand it, plus it only works for sending data between two Arduino Unos. Here is my master:
#include <SPI.h>
float a = 3.14159;
float b = 2.252332;
uint8_t storage [12];
float buff[2] = {a, b};
void setup()
{
digitalWrite(SS, HIGH);
SPI.begin();
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop()
{
digitalWrite(SS, LOW);
memcpy(storage, &buff, 8);
SPI.transfer(storage, sizeof storage ); //SPI library allows a user to
//transfer a whole array of bytes and you need to include the size of the
//array.
digitalWrite(SS, HIGH);
delay(1000);
}
And here is my slave:
#include <SPI.h>
byte storage [8];
volatile byte pos;
volatile boolean process;
float buff[2];
void setup()
{
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPCR |= _BV(SPIE);
pos = 0;
process = false;
Serial.begin(9600);
}
ISR(SPI_STC_vect)
{
byte gathered = SPDR;
if( pos < sizeof storage)
{
storage[pos++] = gathered;
}
else
process = true;
}
void loop()
{
if( process )
{
memcpy(buff,&storage,8);
Serial.print("This is val1:");Serial.println(buff[0], 5);
Serial.print("This is val2:");Serial.println(buff[1], 6);
storage[pos] = 0;
pos = 0;
process = false;
}
}
Any help would be aprreciated, and please understand that I am a newb in this subject.
Im making a project that i scan rfid cards to gain access to a door. I have made the code the way i want it to function but currently its drawing 22mA. I want this project to be powered by an 4Ah Lead acid battery.
I have removed the leds from the reader and from the arduino to lower consumption. Also i run arduino at 1Mhz and that looks that lowered the consumption a lot. Also i disabled Analog converter of arduino.
IM using arduino nano and RFID-RC522 reader
the output is an 5 volt relay.
Here is the code:
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define relay 4
#define led 2
#define button 5
byte readCard[4];
byte storeCard[4];
byte lastCard[4] = {1,2,3,4};
uint8_t successread = 0;
int flag=0;
void setup() {
CLKPR = 0x80;
CLKPR = 0x04;
ADCSRA = 0;
pinMode(relay, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(led, LOW);
SPI.begin();
mfrc522.PCD_Init();
}
void loop() {
do {
successread = getID();
}
while(!successread);
digitalWrite(led, HIGH);
delayMicroseconds(100);
digitalWrite(led, LOW);
lastcard();
if(digitalRead(button)==LOW){
save_delCard();
}
if(checkID(readCard) && !(digitalRead(button)==LOW)){
if(flag==0){
digitalWrite(relay, HIGH);
flag = !flag;
}
else{
digitalWrite(relay, LOW);
flag = !flag;
}
}
}
/////////READ CARD FROM READER/////////////////
uint8_t getID(){
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID
reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get
Serial and continue
return 0;
}
for (uint8_t i=0; i<4; i++){
readCard[i] = mfrc522.uid.uidByte[i];
}
mfrc522.PICC_HaltA(); // stop reading
return 1;
}
///////////////////SAVE OR DELETE CARD/////////////////////////
uint8_t save_delCard(){
uint8_t count = EEPROM.read(0);
int start = (count*4) + 1;
int num = count*4 +1;
if(checkID(readCard)){
uint8_t slot = slotcheck(readCard);
for(uint8_t m=0;m<4;m++){
EEPROM.write((num-m-1),0);
}
for(uint8_t k=0;k<4;k++){
EEPROM.write((((slot-1)*4)+1+k),lastCard[k]);
}
count--;
}
else{
count++;
}
for(uint8_t j=0; j<4;j++){
EEPROM.write(j + start, readCard[j]);
}
EEPROM.write(0,count);
return 0;
}
////////////////////READ FROM MEMORY//////////////////////////////////
uint8_t readID(uint8_t number){
uint8_t start = (number*4) + 1;
for(uint8_t j=0;j<4;j++){
storeCard[j] = EEPROM.read(start + j);
}
}
///////////////////COMPARE IDS///////////////////////////////
bool compare(byte arraya[],byte arrayb[]){
for(uint8_t i=0;i<4;i++){
if(arraya[i]!=arrayb[i]){
return false;
}
else{
return true;
}
}
}
/////////CHECK IF CARD IS IN MEMORY/////////////////////////////////////
bool checkID(byte arraya[]){
uint8_t count = EEPROM.read(0);
for(int i=0;i<count;i++){
readID(i);
if(compare(arraya, storeCard)){
return true;
}
else {
}
}
return false;
}
////////////////FINDS WHERE CARD IS IN MEMORY//////////////////
uint8_t slotcheck(byte arraya[]){
uint8_t count = EEPROM.read(0);
uint8_t slot=0;
for(int i=0;i<count;i++){
readID(i);
slot = i+1;
if(compare(arraya, storeCard)){
return slot;
}
else {
}
}
return NULL;
}
///////////////SAVES LAST CARD/////////////////////
void lastcard(){
uint8_t count = EEPROM.read(0);
for(uint8_t p=0;p<4;p++){
lastCard[p] = EEPROM.read((count*4)-3+p);
}
}
//////////////////////////////////////////////////////////////////
I have Arduino and a Duinotech NRF24L01, I am trying to send the string "Hello world" with maniacs bug RF24 library however, I think it cannot detect the incoming RF signal.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
const uint64_t pipe = 0xF0F0F0F0E1L;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, pipe);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
else {
Serial.println("Data was not found");
}
In the read code, it would always execute data was not found. This makes me think that maybe it does not find the RF signal at all.
Here is the code that writes the data.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
int text = 1;
const uint64_t pipe = 0xF0F0F0F0E1LL;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
radio.stopListening();
}
void loop() {
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
Try this
For the transmitter Instead of const uint64_t pipe = 0xF0F0F0F0E1LL; use const byte address[6] = "00001"; as the address and then have you void setup like the code below
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
Then ensure that you have a value set for the test variable to be transmitted as below
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
At the receiver end have this code running
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text); //This will print out the received value
}
}
PS: Ensure that all the connections are done to the right pins
AND you can test if the NRF24L01 chip is connected correctly by adding the code below
bool result = radio.isChipConnected ();
Serial.println (result);
it should print out a 1 to the serial monitor if the NRF24L01 chip is connected correctly
I have a NodeMCU 1.0 and a ESP8266 12-E I created some code to check a GPS signal and report back, on the NodeMCU the code works 100% no problems but when I load the code on a ESP8266 12-e (with NodeMCU flashed on it) I can not get the GPS signal to show up wired to the same GPOI ports and everything.
Most of the code:
#include <TimGPS.h>
#include <SoftwareSerial.h >
#include "FS.h"
extern "C" {
#include "gpio.h"
#include "user_interface.h"
}
static const int RXPin = 12, TXPin = 255;
static const uint32_t GPSBaud = 9600;
TimGPS gps;
SoftwareSerial ss(RXPin, TXPin);
void printGPSData()
{
Serial.print("Latitude : ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude : ");
Serial.println(gps.location.lng(), 6);
Serial.print("Speed : ");
Serial.println(gps.speed.mph(), 6);
Serial.print("Satellites: ");
Serial.println(gps.satellites.value());
Serial.print("Time UTC : ");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.println(gps.time.second());
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
void setup() {
Serial.begin(115200);
Serial.println("Startup");
ss.begin(GPSBaud);
}
void loop() {
if (millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("No GPS data received: check wiring"));
}
else
{
printGPSData(url);
}
}