Notifications
Clear all

Hi guys i need some help with C++

7 Posts
4 Users
0 Likes
742 Views
10jack2
(@10jack2)
Active Member
Joined: 1 year ago
Posts: 3
Topic starter  

So I am currently working on a wireless controller for an RC car with an arm on top. To be able to control this I want to have 4 joysticks However the Arduino Uno only has 6 analog input pins. So I turned to a multiplexer However I am having trouble converting the array of the values from the pins into a string so I can transmit it. It just makes a list full of jargon/random characters. Before I added the part of the code that adds all the values to a string and prints them. The code was running fine reading the pins and giving me the values.  I am quite new to Arduino and C++ so it is probably an easy fix. code is bellow

//16-Channel MUX (74HC4067) Interface
//===================================
int i;
int pot = A0; int potVal; int S[4] = {1, 2, 3, 4};
int MUXtable[16][4] =
{
  {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0},
  {0, 0, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 0}, {1, 1, 1, 0},
  {0, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 1, 0, 1},
  {0, 0, 1, 1}, {1, 0, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 1}
};

int xpos;
char xpos_list;
char xpos_list2;
int mux_values[16];
//=================================================
void setup()
{
  Serial.begin(9600);
}
//=================================================
void loop()
{
  for (i = 0; i < 16; i++)
  {
    selection(i);
    int xpos = analogRead(pot);
    Serial.print(xpos);
    Serial.print(",");
    xpos_list2 = xpos_list;
    xpos_list = xpos + "," + xpos_list2;
    Serial.print(xpos_list);
    delay(100);
  }
}
//=================================================
void selection(int j)
{
  digitalWrite(S[0], MUXtable[j][0]);
  digitalWrite(S[1], MUXtable[j][1]);
  digitalWrite(S[2], MUXtable[j][2]);
  digitalWrite(S[3], MUXtable[j][3]);
}

   
Quote
Topic Tags
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2235
 

@10jack2

This is very confusing,

- you read xpos from the potentiometer,

- print it followed by a comma to the serial port

- set xpos_list2 to xpos_list (which was never set) 

- set expos_list (which is declared to be a single character) to a short string

- then print out xpos_list (which is still only 1 character long)

- pause for 1/10th second 

 

It's not clear to me what were you trying to print ?

Experience is what you get when you don't get what you want.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1659
 

@10jack2

Maybe xpos_list needs to be a String?

xpos_list = xpos + "," + xpos_list2;

 

 


   
ReplyQuote
10jack2
(@10jack2)
Active Member
Joined: 1 year ago
Posts: 3
Topic starter  

@will The printing is just so I can see what is happening. The final product I want is a comma-delimited string which I can then broadcast. 


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2235
 

@10jack2 

Can you share with us the details about the values to be included, it would help us explain to you how to make the string.

For example, if you just needed the 16 pot readings you'd need to declare

String   theLine; 

before the start of the "for (i=" loop

And then, immediately after the start of the for loop you put

    theLine = "";   // Empty the string

and then after you read and print expos to the serial port you add the lines

    theLine = theLine + string(expos);

    if (i<15) theLine = theLine+",";

Which two lines will add the pot value and a comma (only if it's not the last pot value)

delete all references to xpos_list and expos_list2 and add 

    Serial.println(theLine); // Print to serial monitor

 

After the end of the for loop, you'll have all 16 pot values separated by comms available for your use.

Experience is what you get when you don't get what you want.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1659
 

@10jack2 

 

 

void setup() {
  Serial.begin(9600);
  delay(500);
}

int value;
String message;

void loop() {
  message = "";
  for (int i=0; i<16; i++){
    value = random(256);
    if (i==0){
      message = String(value);
    }else{
      message = message + ',' + String(value);
    }
  }

  Serial.println(message);
  delay(500);

}

 

 


   
ReplyQuote
DaveE
(@davee)
Noble Member
Joined: 2 years ago
Posts: 796
 

Hi @10jack2,

   As @will says, it is difficult to figure out the mistakes, when the only 'specification'  information is some code that does the 'wrong thing'. I also think some specific hardware details might further clarify the situation.

As for the coding, I assume you are trying to read 16 potentiometer values, and then construct a formatted string based on those values to transmit somewhere.

Perhaps some of your difficulty is your loop combines the 'data acquisition' of reading the pots, with 'data formatting' of creating a string, etc 

Maybe you should consider separating these two 'phases' (1 and 2), so each pass through void loop(); does:

  1. Read 16 values into an array
  2. Format and transmit string, using array of values
  3. ... you may want to put a delay at this point to give a sensible data update rate .. see  folllowing notes

Notes:

  • Presumably all 16 pot values represent a single 'time slot'.
    • maybe spilt each analogue read into
      • set up multiplexer 
      • wait for voltage at ADC input to settle
      • read ADC value into array
    • As suggested in the above sub-list, you may need a small delay between setting the multiplexer and reading the voltage, depending upon impedance of the source and capacitance, but that is probably quite short in maybe 1 millisec? ... but obviously this depends on your circuit design. Whilst this will obviously affect the loop time, I would avoid using it to control the overall data 'refresh' rate
  • Following on from the first note, you may need to control the interval between transmitting data 'strings',  to avoid 'flooding the receiving element, and maybe giving time for the data to be transmitted. Hence, after sending the data, consider a delay before leaving the loop()

--------------

summarising into a pseudocode for the 'loop()' part :

void loop()
{
    for i = 1 to 16
   {  
       Set_multiplexer_to_pot(i)
       Short_delay_for_voltage_to_settle();  // if needed
       Pot_val(i) = Read_ADC_val()
   }

  <optionally print Pot_val array to debug screen whilst testing code>

   String_to_send = Convert_Pot_vals_to_string(Pot_val)
   Send_String (String_to_send)


   Long_Delay_to_control_loop_repeat_rate()
}

------------------

Hope this helps. Good luck. Dave


   
ReplyQuote