Go Down

Topic: Bar graph converted to display serial port data (Read 798 times) previous topic - next topic

CrossRoads

Hi gang,
Name is Robert, first time poster, got a new Duemilanove.
After some fits getting the Vista serial port setup, am finally compiling & down loading.
Byte 1 below changes randomly, want to show bits 0-5 as LEDs, don't care about bytes 2-3-4-5.
B0  data here changes, MSB is always 1.
04
1A
2A
3A
Serial port at 4800, stream repeats at 400 Hz. Only bit 0 turns on at 15mS on/10mS off when the byte above is sitting at 80.

Thanks in advance. Code listing to follow after this 'normal' post.

Bob
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

CrossRoads

Code listing.

Byte 1 below changes randomly, want to show bits 0-5 as LEDs, don't care about bytes 2-3-4-5.
B0  data here changes, MSB is always 1.
04
1A
2A
3A
Serial port at 4800, stream repeats at 400 Hz. Only bit 0 turns on at 15mS on/10mS off when the byte above is sitting at 80.

Thanks in advance.
Bob

/*
 LED bar graph -> mangled into Serial -> Parallel output

Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph
uses 10 LEDs, you can use any number by changing the LED count
and the pins in the array.

This method can be used to control any series of digital outputs that
depends on an analog input.

The circuit:
* LEDs from pins 2 through 9 to ground

created 26 Jun 2009 -> mangled 8-22-2010 by Bob Patterson
by Tom Igoe

This example code is in the public domain.

www. arduino. cc/ en/ Tutorial/ BarGraph
*/


// these constants won't change:
//const int analogPin = 0;    // the pin that the potentiometer is attached to
const int ledCount = 8;    // the number of LEDs in the bar graph

int ledPins[] = {
 2, 3, 4, 5, 6, 7, 8, 9 };   // an array of pin numbers to which LEDs are attached
// 11 & 12 also
int ledPin12 = 12;
int ledPin11 = 11;

// add place to store serial data
unsigned incomingByte=0;

void setup() {
 // loop over the pin array and set them all to output:
 for (int thisLed = 0; thisLed < ledCount; thisLed++)
 {
   pinMode(ledPins[thisLed], OUTPUT);
 }
 pinMode(ledPin12,OUTPUT);
 pinMode(ledPin11,OUTPUT);

 //Setup serial port
 Serial.begin(4800); // connect to Serial port on Pin 2, need 4800-N-1
 Serial.flush();  // clear out the incoming register

}

void loop()
{
 // read the potentiometer:
 //  int sensorReading = analogRead(analogPin);
 // replace with new data source, the serial port
 if (Serial.available()>0)
 {
   //    if the port has data, read it
   incomingByte = Serial.read();
   incomingByte=incomingByte & 255; // mask off upper 8 to be 0s
   digitalWrite(ledPin12,HIGH);
   digitalWrite(ledPin12,LOW);

   if (incomingByte > 128);
   //  If the bit 7 = 1, its our lights message
   {
     digitalWrite(ledPin11,HIGH);
     digitalWrite(ledPin11,LOW);

     // map the result to a range from 0 to the number of LEDs:
     int ledLevel = map(incomingByte, 0, 255 , 0, ledCount);

     // loop over the LED array:
     for (int thisLed = 0; thisLed < ledCount; thisLed++)
     {
       // if the array element's index is less than ledLevel,
       // turn the pin for this element on:
       if (thisLed < ledLevel)  
       {
         digitalWrite(ledPins[thisLed], HIGH);
       }
       // turn off all pins higher than the ledLevel:
       else
       {
         digitalWrite(ledPins[thisLed], LOW);
       } // outputs turned off
     } // outputs turned on & off
   }
 }
} // if  any serial data available loop
// end of void loop


Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

James C4S

Quote
Byte 1 below changes randomly, want to show bits 0-5 as LEDs, don't care about bytes 2-3-4-5.
B0  data here changes, MSB is always 1.
04
1A
2A
3A
Serial port at 4800, stream repeats at 400 Hz. Only bit 0 turns on at 15mS on/10mS off when the byte above is sitting at 80.


Your description is very confusing.  Step back and explain what you are trying to do in detail.  Then explain your problem based on that.  The statement "Byte 1 below changes randomly, want to show bits 0-5 as LEDs, don't care about bytes 2-3-4-5" is practically gibberish.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

CrossRoads

Yeah, I am sorry about that. Had a nice explanation all typed up, the forum bounced it on me as a first post, the whole thing got lost and I had to re-do it.

Here's the better explanation.
I have 5 bytes repetitive serial data coming in, I want to turn on the LEDs to represent high bits in one of the bytes, changing only when the bits do. So read in a byte, write it to the outputs pins.
(just a simple serial to parallel converter, seemed like it would be pretty straightforward).

I have a Duemilanove, Code -0018, Windows Vista. I know the Arduino & resisters/LEDs are good as I have tried code to turn them all on for a second or two & then off again before the Void loop.

I have an external box that continously puts out 5 bytes of data, updated at 200 Hz rate, so each byte gets updated at a 40 Hz rate, output level is 0-5V.

The box is a fencing scoring machine, the data represents the information being displayed as lights, I want to remote display the same lights.  
I have pins 2 & 5 from scoring machines 9-pin serial port connected to Arduino's Rx & Gnd.
The first byte has the bits that show left & right lights when a touch is scored, left & right lights when if a touch is scored off target, left & right lights if electrical ground is touched. The 2-3-4-5 bytes have other info I don't care about (weapon type and time info). I only want to duplicate the 6 lights.
Byte 1 always has MSB =1 , the others have MSB =0. so that's pretty easy to screen for.

I can see that the code sits & waits for valid serial data to come in by watching bit 12 on a scope - it starts toggling when I connect the Rx to the Arduino (I have an inline switch as I noted that I couldn't program when the RX was connected.  I realized eventually that my RX might have been getting corrupted by having the USB connected at the same time, so now after downloading I disconnect the USB and plug in external power.)  
I can see that it apparently recognizes valid words coming in by bit 11 toggling.
What I can't seem to get is the LEDs to turn on when the different score lights turn on.
I have hooked up the scoring machine to my PC and see the MSB data change as the lights change.  Typical data is

80 (no lights on)
04
1A
2A
3A
endlessly repeated.

Bit 0 of MSB = touch left
Bit 1 = touch right
Bit 2 = invalid touch left
Bit 3 = invald tough right
Bit 4 = left grounded
bit 5 = right grounded
bit 6 = buzzer

None are exclusive of the other, and is common for more than one to be on.

So, watching the data scroll by on my PC, the 5 bytes above keep scroilling by, and the 80 can be seen to change to follow the lights:

81 (left touch scored, also E0 with buzzer)
82 (right touch scored, also C2 with buzzer)
83 (both touch scored, also C3 with buzzer)
84 (left off target)
88 (right off target)
8C (both off target)
90 (left ground)
A0 (right ground)
B0 (both ground)

just about any combination is possible and can be on for any duration.

So, I've got the Arduino wired up with 10 resisters/LEDs, pins 2-9 to represent the bits in MSB, and 2 more to toggle so I can see where the program is.
Typing this up, I am realizing the bar-graph display is not the right method, as I want multiple lights turned on, and they could very well not be next to each other.

I had tried before this method to bit-mask for the various bits, but it did not seem like the outputs would stay turned on after being written to. Am not sure the masking was working, and I don't think I revised that after I realized that I needed to disconnect the USB while the program was running.

I want to stay away from adding an external latch.  My goal is to port this to a Mini Pro with just a few wires coming off for actual use.
(Serial data, gnd, +5V, the 6 outputs). The external lights will be on another board with +12V, a 7805, and LEDs/transistors for higher current control (not much, 80-100mA for 4 columns of 4-5 LEDs/resistor in a parallel).

Thanks in advance for the help.

Bob


Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

CrossRoads

Got this resolved - see my other post about it here.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282632244/0

Basically, came down to my external device putting out  high true logic, needed to invert it before going into the Arduino UART.

Rewrote code quite a few times before seeing the difference using a scope.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up