I'm moving a thread from the Programming Questions part of the forum to here since it's starting to look more and more like a hardware issue. Most likely my not quite able circuit building skills.
The short story:
I want to control an RGB LED via a RF Transmitter/Receiver pair using VirtualWire. The transmitter sends 0-1023 from a potentiometer to the receiver that uses the incoming data to control the LED. Everything in this circuit works perfect as long as I don't try to analogWrite to the LED. Well it works for a couple of seconds but then fails. Seemingly no transmission gets through to the receiver.
For the long story check the thread in the link above.
This is what I've checked:
The transmitter is not the problem. It continues to send data after the "hang".
The LED is not the problem. It works just fine if I run the code "locally" on the receiver unit with a potentiometer hooked up.
If I just yank out the LED from the circuit the problems disappear. But I guess that comes from breaking the circuit this removing the issue that might be causing problems.
EDIT: Fixed broken links.
Circuit diagram
Breadboarded Circuit 1
Breadboarded Circuit 2
Breadboarded Circuit 3
Since it looks a bit messy I might aswell say, NO! There are no crossed leads touching.
Anyone able to spot anything that might cause the RF receiver to stop receiveing data or atleast disturb the transmission.
Code parts below for the curious.
Code for the receiver
/*
RGB LED controller
*/
#include <VirtualWire.h>
// LED's
int ledPin = 13;
// Sensors
int Sensor1Data;
int potVal;
// RF Transmission container
// char Sensor1CharMsg[5]; // commented out not necessary
// set the ledPins
int ledRed = 6;
int ledGreen = 5;
int ledBlue = 3;
// LED Power variables
byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;
void setup()
{
// sets the digital pin as output
pinMode(ledPin, OUTPUT);
// VirtualWire
// Initialise the IO and ISR
// Required for DR3100
vw_set_ptt_inverted(true);
// Bits per sec
vw_setup(2000);
// Start the receiver PLL running
vw_rx_start();
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
// serial for debugging purposes only
Serial.begin(9600);
}
void loop()
{
dataRX();
colorControl();
// DEBUG
Serial.print("Sensor 1: ");
Serial.print(Sensor1Data);
Serial.print(" potVal: ");
Serial.print(potVal);
Serial.print(" redPwr: ");
Serial.print(redPwr, DEC);
Serial.print(" greenPwr: ");
Serial.print(greenPwr, DEC);
Serial.print(" bluePwr: ");
Serial.println(bluePwr, DEC);
// END DEBUG
} // END loop()
void dataRX()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Non-blocking
if (vw_get_message(buf, &buflen))
{
int i;
// Turn on a light to show received good message
digitalWrite(13, true);
/* Commented out for buffer to integer directly
// Message with a good checksum received, dump it.
for (i = 0; i < buflen; i++)
{
// Fill Sensor1CharMsg Char array with corresponding
// chars from buffer.
Sensor1CharMsg[i] = char(buf[i]);
}
// Null terminate the char array
// This needs to be done otherwise problems will occur
// when the incoming messages has less digits than the
// one before.
Sensor1CharMsg[buflen] = '\0';
// Convert Sensor1CharMsg Char array to integer
Sensor1Data = atoi(Sensor1CharMsg);
*/
// Convert buffer directly to Integer String
Sensor1Data = atoi((const char*)buf);
// Turn off light to and await next message
digitalWrite(13, false);
}
// delay(200);
}
// lightMode 1
void colorControl() {
// read the potentiometer position
potVal = Sensor1Data;
// RED > ORANGE > YELLOW
if (potVal > 0 && potVal < 170) {
redPwr = 255;
bluePwr = 0;
greenPwr = map(potVal, 0, 170, 0, 255);
}
// YELLOW > LIME?? > GREEN
if (potVal > 170 && potVal < 341) {
greenPwr = 255;
bluePwr = 0;
redPwr = map(potVal, 341, 170, 0, 255);
}
// GREEN > TURQOUISE
if (potVal > 341 && potVal < 511) {
greenPwr = 255;
redPwr = 0;
bluePwr = map(potVal, 341, 511, 0, 255);
}
// TURQOUISE > BLUE
if (potVal > 511 && potVal < 682) {
bluePwr = 255;
redPwr = 0;
greenPwr = map(potVal, 682, 511, 0, 255);
}
// BLUE > PURPLE
if (potVal > 682 && potVal < 852) {
bluePwr = 255;
greenPwr = 0;
redPwr = map(potVal, 682, 852, 0, 255);
}
// PURPLE > RED
if (potVal > 852 && potVal < 1023) {
redPwr = 255;
greenPwr = 0;
bluePwr = map(potVal, 1023, 852, 0, 255);
}
// Display colors
colorDisplay();
}
// Displays the colors when called from other functions
void colorDisplay() {
analogWrite(ledRed, redPwr);
analogWrite(ledGreen, greenPwr);
analogWrite(ledBlue, bluePwr);
}
Code for the transmitter
/*
Sensor Transmitter
By Markus Ulfberg 2012-07-06
Takes a sensor reading 0-1023
converts it to a char array and sends
to RF receiver unit via VirtualWire
*/
#include <VirtualWire.h>
// LED's
const int ledPin = 13;
// Sensors
const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;
int Sensor1Data;
char Sensor1CharMsg[5];
void setup() {
// PinModes
// LED
pinMode(ledPin,OUTPUT);
// Sensor(s)
pinMode(Sensor1Pin,INPUT);
// for debugging
Serial.begin(9600);
// VirtualWire setup
vw_setup(2000); // Bits per sec
}
void loop() {
// Read and store Sensor 1 data
Sensor1Data = analogRead(Sensor1Pin);
// Convert integer data to Char array directly
itoa(Sensor1Data,Sensor1CharMsg,10);
/*
// DEBUG
Serial.print("Sensor1 Integer: ");
Serial.print(Sensor1Data);
Serial.print(" Sensor1 CharMsg: ");
Serial.print(Sensor1CharMsg);
Serial.println(" ");
// END DEBUG
*/
digitalWrite(13, true); // Turn on a light to show transmitting
vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off a light after transmission
delay(200);
} // END void loop...