The variables choosenIna and count have crazy random values in the Serial Monitor, every time are different and just happens once every 5 times after rebooting the board.
CODE:
int count = 0;
Int Ina = 0;
void test(inaType)
{
serial.println("count: %d", count);
serial.println("Ina: %d", Ina);
switch (inaType)
{
case 0:
count++;
Ina = 3;
break;
case 1:
count++;
Ina = 4;
break;
}
}
Without your entire sketch there is no way to debug this issue. The problem is likely NOT in this member function! It may have to do with how the variables are defined, scoped, and/or initialized.
I still cannot understand how is it possible that declaring a variable and use it in just that bit of code it can behave randomly the 20% of the times I run it.
I've edited the code so you can see the variables and simplified the unnecessary things.
What if somewhere else in the program you have written to memory that you should not have accessed, typically by writing outside the bounds of an array, then in my experience all bets are off
Please post a complete sketch that illustrates the problem
even if I correct your errors in the modified code snippet #1 it will not show the problem you see.
// demonstration why it is needed to show a full code
// https://forum.arduino.cc/t/variable-gives-random-numbers/1145638
// 2023-07-08 by noiasca
// to be deleted 2023-09
int count = 0;
int Ina = 0; // fixed
void test(int inaType) // fixed
{
Serial.print(" count:"); Serial.println(count); // fixed
Serial.print(" Ina: "); Serial.println(Ina); // fixed
// it would make more sense to print the values after they were modified in the switch/case
switch (inaType)
{
case 0:
count++;
Ina = 3;
break;
case 1:
count++;
Ina = 4;
break;
}
}
void setup() {
Serial.begin(115200);
}
void loop() {
int rnd = random(3);
Serial.print("rnd:"); Serial.println(rnd);
test(rnd);
delay(500);
}
As 4 (!) posters have told you already: post a compileable code showing your problem.
Stripping down the problem to the bare minimum - makes it even easier for you to spot the problem on your own.
And post your sketch in a new post, do not put it in the first post.
Run what? Your questionable snippet? It's entirely possible that you can't replicate your results without the buggy code that gives them, hence you go X-Y about it.
I'll be number 5. Ask better questions to get better help. (This group is So Bad at this...It's not a good look for the professionalism found in the Arduino users.)
It's not hard to make a wild guess that the snippet posted isn't even at all what he's actually running because it's not actually legal C. So we're probably being asked to guess what the problem is based on a retyped interpretation of what you think the problem is.
Int vs. int matters.
You're printing Ina, but using inaType, which is totaly unset in this snippet. Is it set somewhere else? Maybe. Is it set somewhere else and being clobbered? Maybe. Are you enabling DMA and having hardware overwrite it behind your back? Also possible. The only thing we can be sure of is that the snippet posted here will absolutely not even compile, let alone run.
When asking a question, a poster has the opportunity for a few minutes of time from actual experts trying to help. When you burn those minutes letting the helpers guess what you're actually running and executing, it only frustrates everyone. An incoherent subject line (your ~10 word "elevator pitch" opportunity to get the attention of the best qualified helpers is rarely followed by an actually coherent question. This group is so full of examples.
Take advantage of the answers you're getting, especially the tip to use the (awesome) Wokwi emulator thing - it's the easiest way for you to provide a snippet of code, demonstrated a problem, and then let the experts "see" the problem and offer or even develop and create changes.
Thanks for your help, and I'm sorry I couldn´t provide a compilable software, I´ve just been using Wokwi and I could not reproduce the problem, so it means that is coming from somewhere else as it is a really big piece of code, I´ll try to name the variables in a different way to see if something changes, my next question I´ll try my best to explain it better annd provide a compilable software.
Do you understand that Helpers don't ask questions to Get Help?
And btw, microagression noted and hey, not all help here is professional but...
if you feel tht you didn't get your money's worth then where is my time's worth?
Do YOU help anyone with their project? You have 10 posts now total.