use buttonstate via xbee to shut on servo

My little project involves a single servo sweep on an arduino stacked with an xbee shield that occurs when a single char is sent (I'll spare you the purpose for this). It works when I type the char on the other end in the xctu or other telnet environment, but won't when I try stacking the transmitting xbee on an arduino with pushbutton.

#include <SoftwareSerial.h>

SoftwareSerial XBee(2, 3); // RX, TXint ReadingNumber=0;
int button =3;

void setup() {

Serial.begin(9600);
XBee.begin(9600);
pinMode (button, INPUT);

}
void loop() {

buttonState = digitalRead(button);
Serial.println (buttonState);

if (Serial.available())
{
XBee.write(Serial.read());
if (buttonState==HIGH) {
Serial.print ("d");
digitalWrite (LED, HIGH);
}
if (XBee.available())
{
Serial.write(XBee.read());
}

}

}

but won't when I try stacking the transmitting xbee on an arduino with pushbutton.

You mean that it doesn't work when you have the XBee and the switch connected to the same pin. I'm shocked, I tell. Just shocked. Hit it with a big hammer while you tell it to shape up!

I've tried an unused pin but to no success.

debrouillez40:
I've tried an unused pin but to no success.

So, your switch doesn't even work, and you think its a programming issue. Please explain why you think that it is a programming issue. Please explain HOW you switch is wired and why you are doing it the hard way.

The switch works if I just get it to do a task like turn on a light.

Please explain HOW you switch is wired and why you are doing it the hard way.

It is wired in the way one would see in the "Pushbutton" tutorial.

I have no reference point as to "why I'm doing it the hard way". If I had the knowledge of different ways to do this, then presumably I wouldn't be doing it in the present way outlined in said tutorial.

How about just not responding any further, if you're going to be so unpleasant?

How about just not responding any further, if you're going to be so unpleasant?

How about you providing all the details UP FRONT, without referring to a bunch of tutorials that you can't even post a proper link to?

The simple way to wire the switch is to connect one leg to ground and to connect the other leg to the digital pin, with the internal pullup resistor enabled. Why anyone would wire a switch another way boggles the mind.

"The switch works" doesn't tell us anything.

Why are you sending the switch state only when there is Serial data to be read? Why are you only sending the switch state to the serial port? I was under the impression that you wanted to send the switch state via the XBee.

The simple way to wire the switch is to connect one leg to ground and to connect the other leg to the digital pin, with the internal pullup resistor enabled. Why anyone would wire a switch another way boggles the mind.

Why the ignorant (me, in this case) would do such a thing is.... because one is ignorant, which explains why I'm asking for help in the first place. How that's mind boggling? Well...

Why anyone would wire a switch another way boggles the mind.

Since I presume you respond to the posts of others who are rank amateurs, let me cue you in on something: the unlearned will grasp at what information is out there. If there is no prior experience or sufficient context, they will try what is available. Example: "Hmm, need to understand how the button works.....google 'Arduino pushbutton'..... OK, this looks promising.....I'll try that." I'm sure your intelligence and skills are formidable, but you may not be aware that that's how those lacking in sufficient skill, learning, and intelligence will often try to tackle simple technical tasks. Perhaps that will ease your amazement at my foolish posts.

There is no real "data" to be read, beyond an arbitrary symbol ("d", which would be sent by the button going high) that triggers the task to be done on the receiver end. Once again, it works if I use a telnet/xbee on a dongle to send the character.

I was under the impression that you wanted to send the switch state via the XBee.

That is indeed the goal, and I've tried tinkering with the syntax to make that happen, but to no success.

That is indeed the goal, and I've tried tinkering with the syntax to make that happen, but to no success.

So, look at your code. What instance talks to the XBee? What instance talks to the Serial Monitor? Which are you using to send the "d"?

What conditions need to be satisfied to send the "d"? What conditions should need to be satisfied?

What instance talks to the XBee?

I'm assuming "if serial available".

What instance talks to the Serial Monitor?

"Serial.println (buttonState); " ?

Which are you using to send the "d"?

Serial.print ("d"); Perhaps that's a mistake?

I'll spend some time with your suggestions. Thank you.

Also, I realized that the "digitalWrite (LED, HIGH);" is frivolous code (leftover from something I did earlier).

What instance talks to the XBee?

I'm assuming "if serial available".

Wrong. It is the XBee object that talks to the XBee.

What instance talks to the Serial Monitor?

"Serial.println (buttonState); " ?

"The Serial object" is the correct answer, but that's close enough.

Which are you using to send the "d"?

Serial.print ("d"); Perhaps that's a mistake?

Yes, it is. You want the "d" to go to the XBee, not the serial monitor application (unless I missed something).

I'll spend some time with your suggestions.

Good. The answer to the next question is that there needs to be Serial data available to be read AND the switch has to be pressed.

The answer to the next question is that data should be sent if the switch is pressed (regardless of whether there is serial data to be read, or not).