here is the code, i didnt clean up everything since there has been some testing to se if we could come around various problems. But the last try is the uncommented one.
Sender:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int xPin = A1; // slider variable connecetd to analog pin 0
int yPin = A2; // slider variable connecetd to analog pin 1
int firePin = 2;
int joystick[3]; // variable to read the value from the analog pin 0
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup() {
joystick[2] = 1;
pinMode (firePin, INPUT);
Serial.begin(57600);
radio.begin();
radio.openWritingPipe(pipe);
radio.printDetails();
}
// float treatValue(int data) {
//// return (data * 9 / 1024) + 48;
// return (data * (0.5 / 1023));
// }
void loop() {
// reads the value of the variable resistor
//if ( joystick[0] = analogRead(xPin) <
joystick[0] = (analogRead(xPin) /*- 495*/ / 2) -246;
joystick[1] = (analogRead(yPin) /*- 534*/ / 2) -267;
// int temp = digitalRead(firePin);
// if (temp == 0)
// {
// joystick[2] = 1;
// }
// else
// {
// joystick[2] = 0;
// }
joystick[2] = digitalRead(firePin);
Serial.println(joystick[2]);
radio.write( joystick, sizeof(joystick) );
//drive(0);
// this small pause is needed between reading
// analog pins, otherwise we get the same value twice
//// delay(100);
// reads the value of the variable resistor
// joystick[1] = analogRead(yPin);
// digitalWrite(ledPin, HIGH);
// delay (1000);
// digitalWrite(ledPin, LOW);
// delay (1000);
// delay(joystick[0]);
// delay(joystick[0]);
//delay(joystick[1]);
//Serial.println(treatValue(joystick[0]));
// Serial.println(treatValue(joystick[1]));
}
Reciever: we had pin 8 controlling the relay for the fire action this was changed to pin 4 but this doesnt work for some reason. pin 8 got all funky on the voltage when the joystick was pulled up or down. Joystick(2) is actually a separate pushbutton(with pulldown resistor) and not the one on the actual joystick.
we tested pull up/down on pin 8 but to no use, the voltage keeps dancing around 2-3 Volts when moving up/down on the joystick.
pin 4 did not work as a fire pin for some unknown reason.
Reciever for up/down and fire:
#include <Servo.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int joystick[3];
int upperLimitPin = 5; //undecided
int lowerLimitPin = 3; //undecided
int val1 = 0;
int val2 = 0;
int fire = 1;
int firePin = 4;
const int MotorPinA = 7;
const int MotorPinB = 6; //PWM
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void drive(int Speed)
{
if (Speed > 255 || Speed < -255)
{
Speed = 0;
}
Speed *= -1;
if (Speed >= 0)
{
digitalWrite(MotorPinA, 0);
analogWrite(MotorPinB, Speed);
}
else
{
digitalWrite(MotorPinA, 1);
analogWrite(MotorPinB, 255+Speed);
}
}
void setup()
{
pinMode(MotorPinA, OUTPUT);
pinMode(MotorPinB, OUTPUT);
pinMode(firePin, OUTPUT);
pinMode (upperLimitPin, INPUT);
pinMode (lowerLimitPin, INPUT);
// pinMode (lowerLimitPin, HIGH);
//pinMode (upperLimitPin, HIGH);
Serial.begin(57600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
fire = digitalRead(joystick[2]);
// Serial.println(fire);
if (fire == 0)
{
digitalWrite(firePin, 1);
}
else
{
digitalWrite(firePin, 0);
}
val1 = digitalRead(lowerLimitPin);
val2 = digitalRead(upperLimitPin);
// Serial.println(val2);
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( joystick, sizeof(joystick) );
// if (joystick[0] > 254)
// {
// joystick[0] = 255;
// }
// if (joystick[0] < -245)
// {
// joystick[0] = -255;
// }
if (joystick[1] > 243)
{
joystick[1] = 255;
}
if (joystick[1] < -254)
{
joystick[1] = -255;
}
Serial.println(joystick[0]);
//Serial.println(joystick[1]);
}
}
// else
// {
// Serial.println("No radio available");
// }
// if (joystick[0] > 15 || joystick[0] < -15)
// {
// Serial.println(String(joystick[0]) + " = xPin");
// }
if (val1 == 1)
{
if (joystick[1] < -45)
{
drive(joystick[1]);
}
else
{
drive (0);
}
}
if (val2 == 1)
{
if (joystick[1] > 45)
{
drive(joystick[1]);
}
else
{
drive (0);
}
}
if (val1 == 0 && val2 == 0)
{
drive(joystick[1]);
}
if ((joystick[1] <= 45 && joystick[1] >= -45))
{
drive(0);
}
// if (joystick[1] < 45)
// {
// drive(0);
// }
// if (joystick[1] > 45)
// {
// Serial.println("aim down");
// drive(joystick[1]);
// }
//
// if (joystick[1] < -45)
// {
// Serial.println("aim up");
// drive(joystick[1]);
// }
// else if (joystick[1] < 45)
// {
// drive(0);
// }
}