Go Down

Topic: How to condense (make more efficient) coding with port manipulations? (Read 831 times) previous topic - next topic

Marcuswdf

Little bit confused.
"For example, the TSCU plug has 10 pins and 61 possible connection combinations (Referred to as Class). For TSCU class 4, the connections are 1-7 2-8 3-9 5-6"
So what happened to 4 and 10?

As noted earlier, starting with someone else's failed attempt is most likely just a further waste of time.

So the code is working and you just need to finish adding the rest of the combinations in?
Means that for that class pins 4 and 10 are not needed, 1-7 means that a jumper cable connects pin 1 and 7 together. Im scrapping the previous person's work and redoing it by using Arrays.

I plan to make an array of pins as what u mentioned and read the rest however I'm having trouble with the part after reading. How would you suggest to "build a char variable as a map of the reads to show '17283956' "? Is it possible to build a list of 1 column "Class No" and another column of connections (eg"17283956"), if so whats the way to go about doing it? Is there a page or tutorial that shows it? That way can possibly get the array to read through the list and print out the class number by matching the connection. Thereby throwing away the 60 if statements.

Thanks

westfw

diddle pin 1; see if it's shows up as connected to some other pin.  (can more than one pin be connected together ?)  Fill in a byte in your "string", index 0, with the ascii representation of the other pin.  If it's not connected anywhere, fill in 'x'
Repeat for all the other pins.  You should wind up with a 10-digit string.
Quote
For TSCU class 4, the connections are 1-7 2-8 3-9 5-6"
That should yield (remembering that if pin 1 is connected to 7, pin 7 is also connected to pin 1):

Code: [Select]
pin   : 123456789A
result: 789x65123x


So you'd want to have a table of all 61 possible classes, each a 10-digit string, indexed by the class:

Code: [Select]
typedef struct _cst {
   char connectionString[11];
} connectionString_type;

connectionString_type classtable[61] = {
   {""},   //0 Fill in all the other classes!
   {""},   //1
   {""},
   {""},
   {"789x65123x"},  // 4: Here's your example.
   {""}
// :
};


Then you just loop through the table doing string compares, and if you get a match, then that index is your class number.
For extra credit, don't check the pins you've already found to be connected, and have a string like
"789x6ccccx" instead.  Faster, and easier to read, easier to write.
Also consider putting the table in PROGMEM.
Watch your 0-based arrays vs 1-based pin numbering.
Document how it's supposed to work (look at how difficult it is to understand what your predecessor was trying to do!)
Hope there aren't any plane crashes that end up being your program's fault!

Slumpert

Thereby throwing away the 60 if statements.
Westfw gave you a very good example of how to use a table and the formulated value as a lookup, but I think that method would need expanding as you have 61 combinations but likely way, way less "Class"s.

Instead of just using
{"789x65123x"},  // 4: Here's your example.

Have another table of "Class"s so that when you find "789x65123x" in position 4, you lookup position 4 on the "Class" table to get the needed value.

Second option might be to research how those parts are put into classifications in the first place.   You might find that there is a "logical" method and instead of having to brute force lookup every combination, you could apply the logic and calculate the "Class" for your given combinations. (Again assuming way less than 61 Class's)


Marcuswdf

Hi westfw,
Thanks for the helpful advice. I'm still unsure of how the code for reading, joining the values into 789x65123x and reading the values into
pin   : 123456789A
result: 789x65123x
Also not all the combinations have so many connections, some have just for example 2-6 only (pin 2 connected to pin 6), all these combinations are unique to each class so each class would have its on unique connections. Also, some classes have a 3way connection eg 1-2,3-5-9 and some have 5-6,6-8,7-10. Would this method still work?
For your example code, is it placed in voidsetup or above it?
Also what does this mean?:
typedef struct _cst {
  char connectionString[11];
} connectionString_type;

Hi Slumpert,
There are exactly 61 classes with its own unique connection combinations however some are add-ons to the previous eg class 10: 1-2 and class 11: 1-2,3-4. How do I turn the individual readings into the string?

So far my code before your reply(incomplete,still figuring stuff out):
Code: [Select]
#include <LiquidCrystal.h>
 const int rs=13,en=12,d4=11,d5=10,d6=9,d7=8;
 LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int  pinArray[] = {54,55,56,57,58,59,60,61,41,40};
int classArray[][2]= {{1,2634710},{2,2635710},{3,263468710},{4,56710},{5,710},{6,5668710},{7,2634},{8,2635},{9,26},{10,56}};
int i=1;
int count=0;

void setup()
{
  lcd.begin(16,2);
 lcd.print("=== PWC(SEA) ===");
  lcd.setCursor(0, 1);
  lcd.print("Plug Identifier");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STARTING UP...");
  delay(300);
  lcd.clear();
  for (count=0;count<10;count++)
  {
    pinMode(pinArray[count], OUTPUT);
  }
}

void loop()
{
  int a;
  int pincombi ="";
  int b;
  for (count=0;count<10;count++)
  {
   digitalWrite(pinArray[count], HIGH);
   while(a<10)
   a=count+1;
   if(digitalRead(a) ==HIGH)
   {
    lcd.print(a);
    b= pincombi & a;
    lcd.print(b);
   }
  }



}

westfw

Quote
I'm still unsure of how the code for reading
You'd set up all the pins with a pull-up or pull-down resistor.
Then you change one pin to an output and write a LOW (or HIGH, if you used pulldowns.)
Then you read all of the other pins, and see which (if any) now show a "LOW" as well - they must be connected to the pin you have as an output.
Change the output pin back to an input, and go to the next pin.
I think this is essentially what the old code was doing with its PORTx manipulations, but the code to interpret the data was awful...
I'd use something like:
Code: [Select]
setAllInputs()  // set all pins as input
setoutput(currentPin)  // set the current pin to an output
for (pins)
  if pin is not currentPin
    if read(pin) == outputstate
       mark connected
 next currentpin

 (all pseudocode...)


Quote
not all the combinations have so many connections, some have just for example 2-6 only (pin 2 connected to pin 6),
That's fine.  You'd end up with "x6xxxx2xxx" or something...


Quote
Also, some classes have a 3way connection eg 1-2,3-5-9 and some have 5-6,6-8,7-10.
That's harder :-(  I'm not quite sure what to suggest, especially without being able to see all of the info.
Perhaps you can un-use the relfectiveness of connection, so if pin 1 were connected to pin 5 and 10, you'd
have 5xxxAxxxx1 instead of 5xxx1xxxx1 ?  Or either, as long as you're consistent?  Or just handle pairs, and test the 3ways as special cases when applicable?


Quote
Also what does this mean?:
typedef struct _cst {
  char connectionString[11];
} connectionString_type;
that defines a C "structure" consisting of 11 characters, so that you'd easily be able to do something like:
Code: [Select]
    if (strcmp(classes[i].connectionString, connectInfo) == 0)
instead of messing with 2d arrays.  "structures" are an important part of C/C++ programming, although I guess they don't show up in Arduino code very often.  ("Classes" are a kind of structure, sort of.)  It'd be worth learning about...

Quote
int classArray[][2]= {{1,2634710},{2,2635710},{3,263468710},...
I think you'll need to use character arrays.  An int (even a long int) is not big enough to hold 10 digits...

Marcuswdf

Hi westfw,
Would my method posted in the post work best or would yours be most efficient and effective?
How would I use your coding as Iam unsure what it is (I'm a beginner and i don't know alot) and what codes to write along with it.


setAllInputs()  // set all pins as input
setoutput(currentPin)  // set the current pin to an output
for (pins)
  if pin is not currentPin
    if read(pin) == outputstate
       mark connected
 next currentpin
What does "mark connected do" and how can I transform from a single pin digit to the "789x65123x" for example.

for more info i pmed you

westfw

Quote
Would my method posted in the post work best or would yours be most efficient and effective?
I'm sort-of assuming that "most efficient" is not that important.


Um, don't you have someone to help you at the place you're interning?

Marcuswdf

Nope, none of them do Arduino Programming. So I'm kinda alone to do it.

westfw

Ah, but you ought to be at a point where help with C/C++...
A lot of what we're talking about (building up a string and looking it up in a table) is not Arduino specific.
(You might even find some help in a data structures textbook - see "symbol tables.")


Delta_G

Nope, none of them do Arduino Programming. So I'm kinda alone to do it.
Don't you get it?  As long as you guys keep doing it for them the these companies don't hire competent engineers.  Why would they when they can get the Arduino forum to do it for free?   That definitely makes more money than doing the honest thing and hiring someone. 

Y'all just let yourselves be taken advantage of and don't even realize the people you're putting out of work.  For profit companies should pay for this kind of stuff if they're not going to share their profits with us for being the design team. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up