Serial communication-2 analog inputs to 2 PWM outputs (adding a digital input)

Hello all,
I am trying to make a robot controller using two analog joysticks, two arduinos, and two xbee wireless modules. I previously got some help from here: Serial communication-2 analog inputs to 2 PWM outputs - Microcontrollers - Arduino Forum
I have everything working using two of the joysticks(potentiometers) and I can make the robot go forwards and backwards with the joystick controllers.

My issue is I now want to add a third input using the switch attatched to one of the analog sticks. I have the switch hooked up to pin 4 as a digital input and I want it to blink an LED (when pressed) off port 7 of the other arduino. I a have added to my code what I thought would work, but it does not work. With this code, nothing happens to the LED when I click the switch. The LED seems to turn on when I move only one of the joysticks forward. Please help me figure out what I have wrong as I would love to get this LED to blink when the button is pressed down.
Transmitter Side Code:

/* Joystick Controller for Multi Purpose Cart - Transmitter Side
   Weston Hayle  */

//Add Easy Transfer Library to sketch
#include <EasyTransfer.h>
EasyTransfer ET;//Name of Easy Transfer = ET

const int potpin1 = A0;//Recognizes port A0 as potentiometer 1 input
const int potpin2 = A1;//Recognizes port A1 as potentiometer 2 input
const int buttonPin = 4;

int ClickState = 0; //variable for click status of right joystick
//Declare more vzriables
struct SEND_DATA_STRUCTURE{
int Pot1val; 
int Pot2val;
int Click;
};

SEND_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);//Initialize Serial Communication @ 9600 bps (Same as Xbee baud rate)
  ET.begin(details(txdata), &Serial);//Easy Transfer begins reading the data from Tx (Serial)

  pinMode(buttonPin, INPUT);//Initializes pin 4 as input (button)
}

void loop(){

  int val1 = analogRead(potpin1); // Reads value of pot 1 value and stores in val1
  int val2 = analogRead(potpin2);// Reads value of pot 2 value and stores in val1
  ClickState = digitalRead(buttonPin);//Reads State of Joystick Switch
  
  //Map val1 and val2 from 0 to 255 (PWM values)
  val1 = map(val1, 0, 1023, 0, 255); 
  val2 = map(val2, 0, 1023, 0, 255);
  //Transmit the data
  txdata.Pot1val = val1;
  txdata.Pot2val = val2;
  txdata.Click = ClickState;

  ET.sendData();//Sends the three values using Easy Transfer Library 
}

Receiver Side Code:

 /* Joystick Controller for Multi Purpose Cart - Receiver Side Arduino Mega
   Weston Hayle */
//Add Easy Transfer Library to sketch
#include <EasyTransfer.h>
EasyTransfer ET;//Name of Easy Transfer = ET

//Declare variables (Must be the same variabes as SEND_DATA_STRUCTURE of Transmitter Code)
struct RECEIVE_DATA_STRUCTURE{
  int Pot1val;
  int Pot2val;
  int Click;
};

RECEIVE_DATA_STRUCTURE txdata;

void setup(){
  Serial.begin(9600);//Initialize Serial Communication @ 9600 bps (Same as Xbee baud rate)
  // Start reading using Easy Transfer Library
  ET.begin(details(txdata), &Serial);
  
//Change frequency of Pins 9 and 10 to 31000 Hz (Inaudible)
int myEraser = 7;             // this is 111 in binary and is used as an eraser
TCCR2B &= ~myEraser;   // this operation (AND plus NOT),  set the three bits in TCCR2B to 0
  int myPrescaler = 1;         // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.   
TCCR2B |= myPrescaler;  //this operation (OR), replaces the last three bits in TCCR2B with our new value 011
 
 pinMode (9, OUTPUT); // Sets pin 9 as PWM output 
 pinMode (10, OUTPUT); // Sets pin 10 as PWM output

 pinMode (7, OUTPUT);// Sets pin 7 as output for LED
}
//Print/Write Received Values
void loop(){
  if(ET.receiveData()){
    analogWrite(9,txdata.Pot1val);
    analogWrite(10,txdata.Pot2val);
   digitalWrite(7,txdata.Click);
  
  }
}

Note I am using easy transfer library:

Bet you did what 99.99% of noobs do: You wired the button between 5V and pin 4 without a pulldown resistor (10k), so the pin is floating when button is not pressed and saying "HIGH" any time it likes. :slight_smile:
Wire button between GND and pin 4, in setup() write:

pinMode(buttonPin, INPUT_PULLUP);

Pin will read LOW when button is pressed and will be pulled HIGH by the internal pullup resistor when not.

westonhayle:
I previously got some help from here: Serial communication-2 analog inputs to 2 PWM outputs - Microcontrollers - Arduino Forum

You seem to be cross posting again.

...R

@Robin2 How is this cross posting? I am asking a completely separate question about a different topic. Stop trolling me

@JCA34F I believe you are correct I did forget to add that pull-up resistor. Thanks so much I will test it out tonight!

Thanks JCA3F, your solution worked! When i Have the pwm outputs hooked up to an LED they dont go all the way off, do you know why this would happen? They dim and get brighter when I move the joysticks as they are supposed to, just dont go all the way off.

If the PWM value goes below 5, set the PWM pin LOW:

if(ET.receiveData()){
    if(txdata < 5)
    {
       digitalWrite(9,LOW);
    }
    else
    }
       analogWrite(9,txdata.Pot1val);
    }