Analog Write Issue with Xbee

I have an issue with an analog write command. I have two Arduino Uno's. One is the sender and is analog reading a flexi sensor. I then map to 0-255 and send that value out through the xbee.

The second Arduino reads in the serial data and then writes it to analog pin 9.

The problem I am having is the led is not fading smoothly with the changes in force on the flexi sensor. It blinks slowly and I only affect the rate of blink when I increase the load on the flexi sensor.

The following is the code in the sending arduino:

const int pin = 5;  // analog pin 5.  Reads a 0-5v signal
int Reading = 0;  // variable to save reading to.  values = 0-1023
int mapvalue = 0; //value to store mapped reading.  needed b/c analog out is 0-255

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
  //Serial.println("Starting..."); //only seen if the other xbee is being monitored.
}

void loop() {
  
  Reading = analogRead(pin);  //Read the value from the flexi circuit on pin 0
  //Serial.print("Reading: ");  //uncomment for testing purposes
  //Serial.println(Reading); //uncomment for testing purposes
  mapvalue = map(Reading, 0, 1023, 0, 255); //mapping 0-1023 value to 0-255
  //Serial.print("Mapvalue: ");  //uncomment for testing purposes
  Serial.println(mapvalue); //send value to the other arduino for analog ouput
  delay(10);  
}

Code on receiving Arduino:

int incomingByte = 255; // a variable to read incoming serial data into 0-255
const int pin = 9; //analog output pin 9


void setup()  {
  // initialize serial communication:
  Serial.begin(9600);  
  pinMode(pin, OUTPUT);
}
  
void loop() {

  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    Serial.print(millis());
    incomingByte = Serial.read();
    //Serial.print(incomingByte);
    analogWrite(pin, incomingByte); //write value
    Serial.print(millis());
    delay(10);
   }
//Serial.print(outputValue); //monitor what is going on the arduino

delay(10); //wait 2 miliseconds before next loop
}

I suspect that the Serial.read() function is taxing on the cpu, but that does not explain why the led is blinking. It looks to me like the led is off/on. When I press on the flexi it still blinks even though the values the arduino is receiving is ~128. Half brightness. I figure that the LED should never go off. Pin 9 should be held at the last analog.write value until it is updated again?

I plan to see the monitor the analog write signal today. I will find an oscilloscope and watch what it is doing. I will post my findings.

Any help with this problem will be greatly appreciated.

  Serial.begin(9600);

The stone age is over. Pick up the speed.

  mapvalue = map(Reading, 0, 1023, 0, 255); //mapping 0-1023 value to 0-255

That’s a pretty expensive way to divide by 4.

  delay(10);

WTF? I thought you said speed was important.

    delay(10);
    delay(10);

I guess not. Never mind. Forget I posted anything.

Agreing with Pauls, if you can do a direct relation y=ax, and don't need an b factor, like y=ax+b, then you should divide directly. map() require more opperation then divide

Paul,

LOL. All good points. I apply all your recommendations.

Frederic,

I understand. I didn’t really think I just used the function. I will remove the map function.

Everyone,

First I have a resistor and capacitor (10uF) hooked up to smooth the pwm from the analog write.

I hooked up my circuit to an o-scope and found this:

The top trace is of the sending signal before entering the sending arduino. You can see where I pressed on the flexi sensor four different times.

The second trace is of the receiving arduinos output signal from pin 9. It is garbage. It does not relate to the first trace at all.

The third larger trace is of the receiving arduino with the exampels->basic->Fade sketch uploaded to it. This was done as a baseline to prove that we can see smooth trace from the arduino.

Now your pwm is just sending average, as if it was not receiving any thing. Try to put a monitor your serial line on the receiver side to see what you receive.