Go Down

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

Marcuswdf

Im trying to program an arduino board for testing different pin combinations but im unsure if there is a faster way of writing the code instead of typing the same code over and over again. The Bxxxxxxxx changes for the different combinations. Any advice or coding examples are appreciated thanks.

EDIT: pls read the updated posting below :)

6v6gt

Some of the repetition seems unnecessary but anyway you could create a function and pass as a parameter the values which change.
You could also read the port directly and use bit masking instead of multiple digitalRead() statements.

westfw

I can't tell what your code is supposed to be doing.
Why do you write PORTK using direct port IO, and then read it one bit at a time with digitalRead()?

Code: [Select]
if ((PORTK & INTERESTING_BITS_MASK) == PARTICULAR_VALUE_OF_BITS) {
   ...
}



Delta_G

A picture of your code?  Really?  You couldn't copy and paste it?  It's TEXT!!!  Why in the world would you post a picture of it?
|| | ||| | || | ||  ~Woodstock

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

outsider

Working with bits is much easier if you learn Hexadecimal, with a little practice, you learn to recognize bit patterns lots quicker than trying to count 1s and 0s, like 11 1101 0111 1100 = 3D7C.
                                                                                                  3   D    7      C

AWOL

A picture of your code?  Really?  You couldn't copy and paste it?  It's TEXT!!!  Why in the world would you post a picture of it?
Yeah, what he said.

Marcuswdf

Hi all,
I'm new to Arduino Programming. I just took over a Arduino project from a previous intern who couldn't finish it. I'm having trouble in understanding some codes like DDRK and Bxxxxxxxx, if anyone can explain what it means to me? As well as any ways to condense the coding and simplify it? The purpose of this coding is to test Torque Signal Conditioner Unit (TSCU) and Autofeather Unit (AFU) for different possible connections when the TSCU and AFU plug is plugged into the Arduino board.
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 and when a class 4 configured TSCU plug is plugged to the Arduino, the LCD should print out "class 1".

Sorry for the mess of codes, please help and tell me if more details needed, can't post full length of code due to character limitation but its really lengthy and tedious. Thanks


Code: [Select]
/*
  
  *PLUG TESTER*
  
  LiquidCrystal Library
  
 This sketch prints out
 * Plug Type
 * Class
 to the LCD
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;

// Left with 7,6,5,4,3,2 & A1,A2,A3,A4,A5 (11 PINS)
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int i = 1;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  ///////////////////////////////////
  //  STANDARD START UP MESSAGE :  //
  ///////////////////////////////////
  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();
  
  ///////////////////////////////////
  // PLUG CONFIGS INSERT THEM HERE //
  ///////////////////////////////////
  //    FINALIZED - CONNECTIONS    //
  ///////////////////////////////////

  //TEST CONNECTION
}
void loop() {
    while (i==1){
      DDRL = DDRL | B10000000;
      PORTL = B10000000;
      if (digitalRead(45) == HIGH){
        if (digitalRead(49) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 15");
          delay(1000);
          break;
        }
      }
      
      ///////////////////////////////////
      //          TSCU OFFSET          //
      ///////////////////////////////////
      // 4 CONNECTIONS - OFFSET
      
      DDRK = DDRK | B00010111;
      PORTK = B00010111;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH && digitalRead(67) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(64) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 4");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00010111;
      PORTK = B00010111;
      if (digitalRead(67) == HIGH && digitalRead(52) == HIGH && digitalRead(51) == HIGH && digitalRead(68) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(64) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 58");
          delay(1000);
          break;
        }
      }

      // 3 CONNECTIONS - OFFSET

      DDRK = DDRK | B00000111;
      PORTK = B00000111;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH && digitalRead(65) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(64) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 1");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00000111;
      PORTK = B00000111;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH && digitalRead(66) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(64) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 2");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00000111;
      PORTK = B00000111;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(64) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 3");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00001011;
      PORTK = B00001011;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(65) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 5");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00010011;
      PORTK = B00010011;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 6");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00010011;
      PORTK = B00010011;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 6");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00010011;
      PORTK = B00010011;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(67) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 8");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00001011;
      PORTK = B00001011;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(67) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(63) == HIGH && digitalRead(65) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 9");
          delay(1000);
          break;
        }
      }
      DDRK = DDRK | B00010101;
      PORTK = B00010101;
      if (digitalRead(68) == HIGH && digitalRead(69) == HIGH && digitalRead(53) == HIGH){
        if (digitalRead(62) == HIGH && digitalRead(64) == HIGH && digitalRead(66) == HIGH) {
          lcd.clear();
          lcd.print("TYPE: OFFSET");
          lcd.setCursor(0, 1);
          lcd.print("CLASS: 10");
          delay(1000);
          break;
        }
      }
    
      
      else {
        lcd.clear();
        lcd.print("=NO CLASS FOUND=");
        lcd.setCursor(0,1);
        lcd.print("= PLEASE RETRY =");
        delay(50);
        break;
      }
    }
  }

hammy

Those commands relate to direct port manipulation.

The Arduino board has registers for the input/outputs which you can set using "pinmode" to set as inputs or outputs.  You can access these port registers directly using those DDR type commands  as a short cut .

Google "port manipulation Arduino" for some guides on how to do this

Paul_KD7HB

Many times in the past I have encountered your situation and have just thrown out what someone else started, and begun from the original specifications. I have also given the same advice to others who encounter the same situation.

I am giving you the same advice. Don't be wasting hours and days on something that may be fatally flawed in the first place.

Paul

Delta_G

Quote
understanding some codes like DDRK
https://www.arduino.cc/en/Reference/PortManipulation



Quote
and Bxxxxxxxx
Those are just binary numbers. It's better to write them with the normal 0bxxxxxx syntax.  But there's an Arduino header that defines all these that start with the capital B. 

https://www.arduino.cc/reference/en/language/variables/constants/integerconstants/
|| | ||| | || | ||  ~Woodstock

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

Marcuswdf

https://www.arduino.cc/en/Reference/PortManipulation



Those are just binary numbers. It's better to write them with the normal 0bxxxxxx syntax.  But there's an Arduino header that defines all these that start with the capital B. 

https://www.arduino.cc/reference/en/language/variables/constants/integerconstants/
Is there a simpler way to write these codes? The port manipulations make it hard to try to catagorize the different classes. 

westfw

Quote
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 and when a class 4 configured TSCU plug is plugged to the Arduino, the LCD should print out "class 1".
I assume that should be "LCD should print out "class 4""?
So you have 10 connections to the Arduino, and your job is to figure out which pins are connected to "each other", and match that info against "known classes" (of which there are 61)?  That's a relatively thorny problem!
Quote
Don't be wasting hours and days on something that may be fatally flawed in the first place.
I agree.  I don't like that existing code - it really bothers me that it checks for 1 bits but not 0 bits, in addition to its verbosity and lack of clarity.
It'd be good to start with some sort of table describing which connections make up each class...

Marcuswdf

I assume that should be "LCD should print out "class 4""?
So you have 10 connections to the Arduino, and your job is to figure out which pins are connected to "each other", and match that info against "known classes" (of which there are 61)?  That's a relatively thorny problem!I agree.  I don't like that existing code - it really bothers me that it checks for 1 bits but not 0 bits, in addition to its verbosity and lack of clarity.
It'd be good to start with some sort of table describing which connections make up each class...

Yea typo should be class 4. I do have a table listing the classes and connections however i can't post it due to its classified nature sorry. Do you have any suggestions? Currently I'm attempting to use Arrays to do it, not sure if there is a better method out there.

Slumpert

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?

Slumpert

Going back to the confused part.
 If a human was to test the part, would they stick a conductivity meter probe on connection 1 and then move the other probe too connections 2-10 until they found a link or not and then write down which pin was linked and then repeat for each additional pin?

If so that's what I would do in code. 
Make array of pins, set one high and read the rest.
"Build" a char variable as a map of the reads.

Now you litterally have a variable containing "17283956" so then you just need 60 if statements to match up for the correct "Class" for that variable.


Go Up