Pages: 1 2 [3] 4   Go Down
Author Topic: This code works but there must be a better/more elegant way  (Read 2050 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks guys - card index it is - but not today.

goodnight
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setChan (byte chan)
{
  digitalWrite(ChSet1, bitRead (chan, 0));
  digitalWrite(ChSet2, bitRead (chan, 1));                         
  digitalWrite(ChSet3, bitRead (chan, 2));
}
Uncompiled, untested, but should save a fair amount of code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
record_type record[4];
So, you have 4 elements in the array.

Code:
  record[0].Ch1 = 1; record[0].Ch2 = 2; record[0].Ch3 = 3; record[0].Ch4 = 4; record[0].Ch5 = 5;  record[0].Ch6 = 6; record[0].Ch7 = 7; record[0].Ch8 = 8;
  record[1].Ch1 = 5705; record[1].Ch2 = 5685; record[1].Ch3 = 5665; record[1].Ch4 = 5645; record[1].Ch5 = 5885; record[1].Ch6 = 5905; record[1].Ch7 = 5925; record[1].Ch8 = 5945;
  record[2].Ch1 = 0; record[2].Ch2 = 1; record[2].Ch3 = 0; record[2].Ch4 = 1; record[2].Ch5 = 0; record[2].Ch6 = 1; record[2].Ch7 = 0; record[2].Ch8 = 1;
  record[3].Ch1 = 0; record[3].Ch2 = 0; record[3].Ch3 = 1; record[3].Ch4 = 1; record[3].Ch5 = 0; record[3].Ch6 = 0; record[3].Ch7 = 1; record[3].Ch8 = 1;
  record[4].Ch1 = 0; record[4].Ch2 = 0; record[4].Ch3 = 0; record[4].Ch4 = 0; record[4].Ch5 = 1; record[4].Ch6 = 1; record[4].Ch7 = 1; record[4].Ch8 = 1;
And, you initialize all 5 of them. Fail!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@UkHeliBob - Wow! - thank you but unhelpful. If you are finding this topic too difficult then please feel free put your feet up and watch the tele instead.

Wow, another thread spirals out of control while I am working on PHP stuff. Amazing! Better cut down the snaky comments if you are initializing 5 elements in an array of 4 items.

How to use this forum
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3516
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't worry Nick.  We have kissed and made up.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Channel 1 5705 Mhz = 000
Channel 2 5685 Mhz = 001
Channel 3 5665 Mhz = 010
Channel 4 5685 Mhz = 011
Channel 5 5885 Mhz = 100
Channel 6 5905 Mhz = 101
Channel 7 5925 Mhz = 110
Channel 8 5945 Mhz = 111
Another thing that's going to bite you on the arse is that channel 2 and channel 4 seem to share the same frequency.
Also, you should revisit the datatypes of your input and output pin designators - "boolean" may well work, but they are not inherently boolean values.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Guys - I am mostly away until Sunday. I will look at all the help and come back to you - I was dreaming about arrays of structs last night - or was it a nightmare!

@AWOL - Channel 2 and 4 - Yeh another typo!! Learning all the time.

I think have got my head around the "struct" bit, I'll put up (my version of) the code later and give you all a laugh!

regards

Alan
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please don't let your eyes glaze over and ignore this code.

If you don't want to understand it now it could be useful in the future.

Code:
#define ENTRIES(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

struct record_t
{
    union
    {
                 int  _v[8];
        struct { int _chan1, _chan2, _chan3, _chan4, _chan5, _chan6, _chan7, _chan8; };
    };

    // non-parameterized constructor
    record_t()
        : _chan1(0), _chan2(0), _chan3(0), _chan4(0)
        , _chan5(0), _chan6(0), _chan7(0), _chan8(0)
    {}

    // parameterized constructor
    record_t(int chan1, int chan2, int chan3, int chan4, int chan5, int chan6, int chan7, int chan8)
        : _chan1(chan1), _chan2(chan2), _chan3(chan3), _chan4(chan4)
        , _chan5(chan5), _chan6(chan6), _chan7(chan7), _chan8(chan8)
    {}
};


// declaring an array of 'record_t' will call the non-parameterized version of
// the constructor for 'record_t'

record_t    record[7];

void setup()
{
    // ... populate the array 'record' at index 0 ...
    const int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    for ( size_t i = 0; i < ENTRIES(array1); i++ )
    {
        record[0]._v[i] = array1[i];
    }

    // ... populate the array 'record' at index 1 ...
    const int array2[] = { 5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945 };
    for ( size_t i = 0; i < ENTRIES(array2); i++ )
    {
        record[1]._v[i] = array1[i];
    }

    // ... populate the array 'record' at index 2 ...
    const int array3[] = { 0, 1, 0, 1, 0, 1, 0, 1 };
    for ( size_t i = 0; i < ENTRIES(array3); i++ )
    {
        record[2]._v[i] = array1[i];
    }

    // ... populate the array 'record' at index 3 ...
    const int array4[] = { 0, 0, 1, 1, 0, 0, 1, 1 };
    for ( size_t i = 0; i < ENTRIES(array4); i++ )
    {
        record[3]._v[i] = array1[i];
    }

    // ... populate the array 'record' at index 4 ...
    // ... from end of 'array5' to start of 'array5'
    const int array5[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
    for ( size_t i = ENTRIES(array5); i--; )
    {
        record[4]._v[i] = array1[i];
    }

    // ... populate the array 'record' at index 5 using the parameterized
    // version constructor of 'record_t' ...
    record[5] = record_t(0, 1, 2, 3, 4, 5, 6, 7);

    // ... array 'record' at index 6 was initialize to 0 using the
    // non-parameterized constructor at declaration ...
}

void loop()
{}
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@lloyddean

Thanks - I do want to understand the code and will work through the replies, all of which have exceeded my present level of skill and understanding. I have yet to experience the Eureka moment. With an array of structs, an existing set of fixed data and not having even used arrays before it might take some time. Thank you all for your patience.

Alan
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK  - in card index form this time - here is the laugh I promised:

Code:
typedef struct
  {
      byte Channel_Number;    //Number in range 1-8
      int Channel_Frequency;  //Whole Number in range 5705 to 5945 - I think that data type "Float" would allow me to use a decimal point to display in GHz instead of MHz.?
      boolean Output_Pin1;    // The pin is either HIGH or LOW, 1 or 0 - However, could also be type "byte" (same size)
      boolean Output_Pin2;    // Ditto
      boolean Output_Pin3;    // Ditto
   
  }  record_type;  //What does this do? I think it says thay the struct i.e. all the data is to be treated as a single (array like) record?

record_type record[9];  //I think this line names the record type "record" with a storage size of 9 (arrays) ?

void setup()
{
  record[0].Channel_Number = 1; record[0].Channel_Frequency = 5705; record[0].Output_Pin1 = 0; record[0].Output_Pin2 = 0; record[0].Output_Pin3 = 0;
  record[1].Channel_Number = 2; record[1].Channel_Frequency = 5685; record[1].Output_Pin1 = 1; record[1].Output_Pin2 = 0; record[1].Output_Pin3 = 0;
  record[2].Channel_Number = 3; record[2].Channel_Frequency = 5665; record[2].Output_Pin1 = 0; record[2].Output_Pin2 = 1; record[2].Output_Pin3 = 0;
  record[3].Channel_Number = 4; record[3].Channel_Frequency = 5645; record[3].Output_Pin1 = 1; record[3].Output_Pin2 = 1; record[3].Output_Pin3 = 0;
  record[4].Channel_Number = 5; record[4].Channel_Frequency = 5885; record[4].Output_Pin1 = 0; record[4].Output_Pin2 = 0; record[4].Output_Pin3 = 1;
  record[5].Channel_Number = 6; record[5].Channel_Frequency = 5905; record[5].Output_Pin1 = 1; record[5].Output_Pin2 = 0; record[5].Output_Pin3 = 1;
  record[6].Channel_Number = 7; record[6].Channel_Frequency = 5925; record[6].Output_Pin1 = 0; record[6].Output_Pin2 = 1; record[6].Output_Pin3 = 1;
  record[7].Channel_Number = 8; record[7].Channel_Frequency = 5945; record[7].Output_Pin1 = 1; record[7].Output_Pin2 = 1; record[7].Output_Pin3 = 1;
 
//The next line is a guess but it compiles in this form - why record [8] apart from it is the next numeric increase and won't compile without a number? ( Giving a record storage size of 9 - see above)
 
  record[8] = (record_type) {0,1,2,3,4};

/*
What is this - my guess is that it holds the output of the array of structs ? Is it correct and, if so, how do I use the information?

Like this?   record[channel number] {array cell 1, array cell 2,.....};?
*/
}

void loop()
{
 
}


I have almost got my head around the "array" and "structure" elements but still no "Eureka" moment. I am aware that there are a number of replies with code suggestions that don't feature here but I have yet to get past this point.

regards

Alan
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
     boolean Output_Pin1;    // The pin is either HIGH or LOW, 1 or 0 - However, could also be type "byte" (same size)
Not could. Should.

While boolean and byte are the same size, boolean implies something about the contents of the variable (true or false) and the context in which it will be used that are NOT appropriate for your variables.

Code:
//What does this do? I think it says thay the struct i.e. all the data is to be treated as a single (array like) record?
The typedef statement assigns a more intuitive name (at least, that is the intent) to an existing type. The existing type is anonymous struct which is hard to use elsewhere in the program, so it is necessary to assign it another name. One that IS usable later.

By creating a named structure (the name goes between struct and the {), the typedef is not as necessary. Still, it is often easier to type "someName" for a type than "struct someName".

Code:
record_type record[9];  //I think this line names the record type "record" with a storage size of 9 (arrays) ?
It creates an array (that is what the [] on the end means) named record, of type record_type (the typedef'd name of the anonymous structure).

Code:
//The next line is a guess but it compiles in this form - why record [8] apart from it is the next numeric increase and won't compile without a number? ( Giving a record storage size of 9 - see above)
You have an enter key, don't you. Use it. Mile long comments are a pain in the ass to read.

There are several ways to initialize a struct instance. You can assign values to each element of the struct, as you did in the previous lines, or you can assign values to all elements in the struct by providing a list of values in the proper order, as in the next line of code.

The second way works only when initializing the array object the first time.

Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS  thank you - got most of that:

boolean - use only when TRUE or FALSE is relevant in the program. Otherwise, byte is appropriate, particularly in an ON - OFF hardware environment.

The first instance of Record_Type gives the anonymous struct a defined name that can be used later. The second instance (Record_Type Record[]) confirms that the data types defined in the struct "Record_Type" are intended to be used as an array called "record[]" The number "[9]" defines the storage space within the array.

Verbose commenting mode now off.

The line "record[8] = (record_type) {0,1,2,3,4};" is a one-off initialisation of the array object. So the contents of the array of structs can be expressed as I thought?

I.E.  record[] = (record_type) {0,1,2,3,4}; giving something like record[1] (record_type[1]) = "5705"

My apologies if the concept or syntax or both are wrong.

regards

Alan
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I.E.  record[] = (record_type) {0,1,2,3,4}; giving something like record[1] (record_type[1]) = "5705"
You need a value in the brackets, because you are not creating a new array with one element. You are initializing one element of an array. The values in the {} are used to initialize the members of the structure, in the order that they appear in the structure (Channel_Number, Channel_Frequency, Output_Pin1, Output_Pin2, and Output_Pin3).
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 21
English
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS - Sorry to be thick. If it answered my question I didn't understand how. Could you offer an example with the appropriate syntax.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could you offer an example with the appropriate syntax.
Your code had an example.

Code:
  record[8] = (record_type) {0,1,2,3,4};
Logged

Pages: 1 2 [3] 4   Go Up
Jump to: