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.

