OK I finally did it! i will call this project
DIY wireless follow focus 28byj stepper motor arduino uno arduino nano pot potentiometer rf24 project xD
Thanks Robin2 for all your inspiration, and thank you paulS, both were great inspirations.
Here is my final code
Receiver:
//#include <AccelStepper.h>
#include <MultiStepper.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Stepper.h>
//Declaremos los pines CE y el CSN
#define CE_PIN 9
#define CSN_PIN 10
#define STEPSREV 2048 // 64(fullsteps) * 64 (reduction ratio)
#define COIL1 4
#define COIL2 5
#define COIL3 6
#define COIL4 7
#define POT 0
#define ENER 13
#define TIMEOUT 3000 //Turns off after 3 secs of inactivity.
//Variable con la dirección del canal que se va a leer
byte direccion[5] ={'c','a','n','a','l'};
//creamos el objeto radio (NRF24L01)
RF24 radio(CE_PIN, CSN_PIN);
//vector para los datos recibidos
float datos[3];
Stepper myStepper(STEPSREV, COIL1, COIL3, COIL2, COIL4);
int PotVal;
int LastPotVal= 0 ; // To implement a software Low-Pass-Filter
int pos = 0; // stepper position(0-4096)->(0-360°)
unsigned long stamp = 0; // last move time stamped.
void setup()
{
//inicializamos el NRF24L01
radio.begin();
//inicializamos el puerto serie
Serial.begin(9600);
//Abrimos el canal de Lectura
radio.openReadingPipe(1, direccion);
//empezamos a escuchar por el canal
radio.startListening();
myStepper.setSpeed(4); // set the motor speed to 4 RPM
pinMode(ENER, OUTPUT); // status led (coils energized).
}
void loop() {
uint8_t numero_canal;
//if ( radio.available(&numero_canal) )
if ( radio.available() )
{
//Leemos los datos y los guardamos en la variable datos[]
radio.read(datos,sizeof(datos));
//reportamos por el puerto serial los datos recibidos
Serial.print("POTENCIOMETRO " );
Serial.println(datos[0]);
}
else
{
Serial.println("No hay datos de radio disponibles");
}
//delay(1000);
int PotVal = datos[0];
PotVal= map(PotVal,0,1023,0,2048); // Map pot range in the stepper range.
PotVal= PotVal * 0.1 + LastPotVal * 0.9 ; // Filtering to reduce noise.
LastPotVal= PotVal;
if(abs(PotVal - pos)> 4){ //if diference is greater than 4 steps.
if((PotVal - pos)> 0){
digitalWrite(ENER, HIGH); //Motor energized.
myStepper.step(1); // move one step to the right.
pos++;
}
if((PotVal - pos)< 0){
digitalWrite(ENER, HIGH); //Motor energized.
myStepper.step(-1); // move one step to the left.
pos--;
}
stamp = millis(); // stamp actual time.
}
else {
if((millis() - stamp) > TIMEOUT){ //Turn Off coils after TIMEOUT.
digitalWrite(COIL1, LOW);
digitalWrite(COIL2, LOW);
digitalWrite(COIL3, LOW);
digitalWrite(COIL4, LOW);
digitalWrite(ENER, LOW); //Motor de-energized.
}
}
}
TRANSMITTER:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//Declaremos los pines CE y el CSN
#define CE_PIN 9
#define CSN_PIN 10
//Variable con la dirección del canal por donde se va a transmitir
byte direccion[5] ={'c','a','n','a','l'};
//creamos el objeto radio (NRF24L01)
RF24 radio(CE_PIN, CSN_PIN);
//vector con los datos a enviar
float datos[3];
void setup()
{
//inicializamos el NRF24L01
radio.begin();
//inicializamos el puerto serie
Serial.begin(9600);
//Abrimos un canal de escritura
radio.openWritingPipe(direccion);
}
void loop()
{
//cargamos los datos en la variable datos[]
datos[0]=analogRead(0);
//enviamos los datos
bool ok = radio.write(datos, sizeof(datos));
//reportamos por el puerto serial los datos enviados
if(ok)
{
Serial.print("Datos enviados: ");
Serial.print(datos[0]);
}
else
{
Serial.println("no se ha podido enviar");
}
//delay(1000);
}
This is to people who is like me and surfs the web in search of codes already written XD. You just need to install libraries, and change the values acording to your needs, pay attention to the stepper motor pins, and connect them on your board accordinly.
Thank you again Robin2