0
votes

I am trying to implement Charlieplexing on my Arduino. At the moment I use 3 outputs to control 6 LEDs, and it is working okay. The problem is that I have this reappearing bug that seems to come and go as I place delays in one of my functions. The bug causes my serial input (through the Arduino USB cable) to mess up if I place some delays in a, more or less, unrelated function. I am controlling the Charlieplexing array by sending a number between 1 and 6 through the Serial port.

if(Serial.available() > 0)
 {
  tempChar = 0;

  char temp = char(Serial.read());

  tempChar = atoi(&temp);

  Serial.println(tempChar, DEC);
 }

This is then converted to an integer which is then interpreted by a function and converted to the right port state for each of the three input/output ports.

void Charlie(byte input)
{

 if(input == 1)
 {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);

  digitalWrite(2, 1);
  digitalWrite(3, 0);
  digitalWrite(4, 0);

 }
else if(input == 2)
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);

  digitalWrite(2, 0);
  digitalWrite(3, 1);
  digitalWrite(4, 0);

 }... (continues to input == 6)

The above works very well, but in will only turn on a single LED at a time. To turn more on I will have to make the Arduino shift between the LEDs to make it seem like more are turned on at a time. For this I created a new function:

void Range(byte input)
{

  if(input == 1)
  {
   Charlie(1);

  }
 else if(input == 2)
{
  for(byte i = 1; i < 3; i++)
  {
   Charlie(i);
  }

}
 else if(input == 3)
{ 

 for(byte i = 1; i < 4; i++)
 {
  Charlie(i);
  delay(4);
 }
}... (Continues to input == 6

This is where the problem appears. If I do not include a delay() after calling my Charlie() function, the Serial INPUT breaks. Now the really strange thing is that the delay does not have to be anything, aka delay(0) will fix my problem.

When I send some input through the serial interface, the Arduino will spit it out again. So if I send the numbers 1 through 6 it will look like this in the serial console:

1 2 3 4 5 6

That is what is expected. Now if I introduce the Range() function without the right delays the out put will look look like this:

15 25 35 45 55 65

Which will break the program.

I know this is kind of vague, but I find it really hard to explain. Hopefully it will make sense to you.

The complete code can be found here: http://pastebin.com/t7tdtfid

UPDATE

I tried running my program through AVR Studio 5 and it was not affected by the problem.

1
What do you exactly mean with "running through AVR Studio"? Are you adjusting the Code to plain C then? If so, please also paste that code.A.H.

1 Answers

1
votes

The first thing to fix is this:

char temp = char(Serial.read());
/*byte*/ tempChar = atoi(&temp);

The atoi functions want a \0 terminated array of char. In your case you can be lucky if there is a 0-byte somewhere after the your single character. And since you don't know what the compiler has put after your temp variable and when this unknown thing changes its state you are....

So please try this code instead:

char temp = char(Serial.read());
tempChar = temp - '0';

Then report if that has helped.