Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

G'day all.... :-)

Lamboom

Coder
Hi .. Not entirely new to C coding ... I generally follow the manuals, as the only need for coding is to work with PLC's, HMI's, and Automation drivers for linear motors. These systems are well designed for the "coding impaired" ... Kinda like you don't really need to know how a computer works, in order to use one. But every so often some bit of "C" statements are required for things to go well in an Automation design. But, I'm not a designer, just a hobbyist that loves these toys ... I use a linear motor to make waves in a long glass tank of water in order to demonstrate the nature of ocean waves and their reactions to different shore models... also their effect on different floating model structures.

To the point: sometimes I want to make a random selection of specifically designed waves.. just to show a confused storm sea.. easy when using a random generation of the variable x which assigns the "Case" number in a "Switch" routine ... That works just fine ... no help is required there.

But then... I'm thinking I would like not to randomly select the case numbers .. I might want to use the cases in a specific order like 9,9,6,8,1 then start over or.. stop. It's been a while .. can you suggest a way to generate that number sequence in "C" for each time the Switch sequence is run?

Perhaps a variable "t" is incremented by 1 with each Switch cycle, then an if statement assigns the value of "6" if t equals 3 ?

You can see why I can't use BBcode yet :) Regards, Michael Lambert
 
So, skipping all the stuff about motors and waves, which is interesting but hardly relevant, it sounds what you want is generate sequence of numbers with a specific pattern ? What is stopping you, assuming you know exactly what you want ? Your last-but-one line would result in 1, 2 , 3 , 4 , 5 , 3 , 4 , 5 , 3 , 4 , 5 , 3 , 4 , 5.... which does not seen very interesting.
But this is confusing... I'm not sure what you mean with "Case" numbers, "Switch routine", "Switch sequence", "Switch cycle"... You're not talking about the C switch statement are you ? Maybe some example or pseudocode would help.
 
Hi cbreemer ..Thanks for the reply! Yes... I am using a Switch sequence .. there are 10 cases in the sequence .. I want to run 8 - 1 - 9 - 6 - 8 in that order
... normally the case number is created randomly from 1 to 10. I would like to select the cases in a sequence of 6 at a time. The code for each case assigns a period or time and an amplitude value .. all that is finished and works fine (and not relevant :)... when selecting the case numbers randomly, with the selections running continuously. Again, I just want to run just 6 cases, then stop.

I assumed a series of IF statements would do it. Something that would count the number of times switch (X) was run, say "i" for increment ... then the IF statements would identify which i and assign the correct value for X... (the case which would run). Example: when i was equal to 3, X would equal 9... and case 9 would run ... then i would be increased to 4 before a new case was to be run and so-on.

It's important to know that this is a "C Type" code for a Red Lion G3 HMI which helps the machine do amazing things not normally found in an HMI.
Unfortunately, I'm not an automation engineer .. i just play one in my barn... :) My question is mostly about the IF statement series ... am I approaching the solution by using a column of If statements?.. or should I try incorporating another Switch ( x ) with 6 cases .. just for creating "out-of-sequence case numbers? Does the increment count and IF statements sound like the way to go? ... and, Thanks again.. Regards Michael Lambert
 
ThReflection 1 of 2  .jpgReflection 2 of 2.jpgis might help a little:

The question is all about how to call cases 8,1,9,6,8 .. a 6th would call a reset program...
 

Attachments

  • Reflection 1 of 2  .jpg
    Reflection 1 of 2 .jpg
    119.2 KB · Views: 1
Ok that clears up much about my confusion about your terminology :)
But please do NOT post screenshots of code. Post the real code, as much as is needed to reproduce the issue, in Code tags. This is a requirement in any programming forum.
 
OK... Thanks for the update. I don't think I can post the "real" code. The coding is done using the Red Lion HMI's Database software... and you would have to run it using an emulator with their software... I should have taken this question to "equipment" Forums like MrPLC or PLCs.net, members that could answer this question, would have that software .. and I would include the database in the posting,, Thanks again, I thought this would be an easy question for C coders; however, perhaps there's more to it because of the equipment using it... Regards, Michael Lambert
 
It would be an easy question for C coders, if only it was clear what exactly you want and if we got full disclosure on where you stand with your C code. As it is we are getting only screenshots of code snippets without seeing how it all hangs together. I know you are doing your best to explain, and you know I am doing my best to help, but it just does not hit home with me like this, sorry. IMO any "equipment" seems totally irrelevant to this, it is ( or should be) a simple programming issue. But by all means also try these other forums, the folks there might be more accustomed to this kind of coding.

This must be a special flavor of C, because for assignments you sometimes use = which is valid C, and sometimes := which is not valid C. Who knows what other surprises this language has ! As for a sequence of if statements within a switch case, there's nothing against that in standard C, and you could also use another switch statement instead of that - it is a matter of style and taste. Have you actually tried it yet, and did it work ?
 
Hi again... Yes I have tried it and it does work .. sort-of ... The increment "I" goes to 1, the first IF statement makes X=8 ...and "switch" selects "8" as it should ... and wave "8" runs ! .. but it keeps running, and " i " doesn't increment to 2 ... so the case 8 keeps playing.

The problem may be a simple syntax error with the == , or := ... I'm very weak in C coding :-(

Here's the thing... If you are willing to look at that first photo showing the stacked IF statements you see i := i + 1

That line used to be x := (Random 10) for Switch (X) which followed .. and we were off and running with a random selection of X (forever) and it all worked fine. What I'm doing is trying to select X in a specific order, and limited to 5 or 6 of them, and then the switch statement calls the Stop program.
If statements seemed like a good idea at the time.. the natural loop which worked great making random values of X (between 0 & 9) doesn't seem to work in order to let i := i +1 ..... I guess I could try i = i + 1 or i == i +1 :) Thanks for thinking about this .. It's just a hobby at this point: https://www.microoceans.com/
 
Try i++ which is the preferred way in C to add one to a value. I don't trust your i := i+1. As this is not valid C, I would not be surprised if your wondrous C compiler (interpreter?) skips it. Do NOT try i == i+1. That is not an assignment but a boolean expression (comparison) which always evaluates to false.
 
Good thoughts.. tried it: i = i ++ .. and the compiler didn't like it. I did try i = i + 1 .. and that worked... but, still the same problem with the increment not advancing by 1 when the cycle repeats. I will let you know what works when it does.. but, I can see your Forum is dedicated to pure C, C#, C++ and the like... perhaps I should read the Red Lion Crimson 3.0 software manual .. they should cover the C syntax they use. I have a feeling I'm close ... but, as you know in coding "close" isn't good enough... :) Thanks ... to be continued. Regards, Michael Lambert
 
Last edited:
My suggestion was to try i++ , not i = i++ ❗ The latter is valid C (and it's strange your wondrous compiler rejects it) but it doesn't do anything. Strange as it may sound, this does not increment the value of i ! C is a very peculiar language and you'll need to study a textbook or do a tutorial to understand this stuff.

So do either
i = i+1;
or
i++;
Both will increment i. The second way is what is common in C.
Did you know that this forum has an IDE (link in the top banner) where you can try out code in many languages including several versions of C ? Very handy to quickly try out statements like this.
 
I got the same result with i++ as I did with i = i + 1 ... so the problem is more complicated... I'll get back to you when it's solved after I work with some people that know both the HMI and the LinMot drive it controls... Remember originally, when X for Switch (X) is created by a Random number generator, and that value is assigned to X before the Switch (X) is called... which worked great. Now, I'm trying to replace that generator... with something that assigns 1 of 6 specific numbers, in a specific order to X. each time Switch (X) is called.. I have a friend in Australia that helped write the original program, hopefully he will get back to me soon. I think I made a big mistake thinking this would be easy... :)
 
Ok, good luck. I might have been able to help had I just seen the code. But I'm still in the dark about what happens where with i and X.
I would recommend that you make up your mind whether to use := or = for assignment. So far the usage seems willy-nilly. The single equal sign is the right way to do it in C. But perhaps your compiler has different ideas...
 
Ya know.. methinks the IF statements are working .. both with i++ and i = i+1 The problem is, I'm not understanding how fast the program is running It may be cycling every micro sec. "i " is an increment that is supposed to count the number of times the value of X has been used by the Switch(X) statement. The first time i is zero and is advanced to 1 .. the IF statements see "1" and assign X the value of 8 ... then Switch (8) sets the values of some parameters. At the Switch sequence end, the parameters are sent to the LinMot Drive which controls the linear motor for one cycle of a stored "curve". When that curve finishes, The value of "i" is again advanced ... to 2, and the IF statements will assign the value of X to be 1 .. and so-on for a total of five selections of X, and thus 5 special curves in a special sequence, are run by the LinMot Drive. When "i" becomes 6, the final parameter issued by Switch(6) is a program that stops the run and resets some parameters. Unfortunately, I think the program in the Red Lion HMI (which has the IF statements) continues to run, cycling every microsecond or so ... because what I see on the HMI when the program is run, (which has the values of X and i displayed), shows i equal to 6 and X equal to 8 ... and curve 8 is running on the linear motor (which is also the last curve in the 5-curve run) sigh! I must study more on how the HMI and LinMot drive interact. Thanks again...
 

New Threads

Buy us a coffee!

Back
Top Bottom