Learning arduino coding, my code should show '40' printed out infinitly in the serial moniotr but only gets printed once. what is wrong with my code?

hi I am doing a course on Arduino programming and this code is supposed to print 40 infinite times in the serial monitor. Yet when I upload the code, it only gets printed once in the serial monitor. I am using an Arduino uno. Why is my code not working?


int encodeNumber(int number)
{
  return (number + 3) * 5;
}

void printEncodedNumber(int number)
{
  Serial.println(encodeNumber(number));
}


int number1 = 5;
int number2 = 6;

int encodedNumber1;

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  encodedNumber1 = encodeNumber(number1);
  int encodedNumber2 = encodeNumber(number2);

  printEncodedNumber(number1);

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(encodedNumber1);
}

I've tested it on a Nano (no Uno at hand at this moment) and it indefinitely prints 40.

Original Uno or clone?
Which operating system?

I am using one sent from my school (it says elegoo uno r3) and I am using windows 10 Arduino IDE (latest version which is from the windows store 1.8.15.

The elego uses the same TTL-to-USB converter as the genuine Uno; so I've removed the dust from my genuine Uno and tested your code; again it behaves as epected. Using IDE 1.8.13 but I don't think that that matters.

  1. Maybe add a small delay (e.g. delay(100);) at the end of loop() might solve the problem.
  2. Maybe changing to a normal install instead of the windows store version might do the trick; the windows store version seems to have some problems at occsion.

Hi,

I reinstalled Arduino as suggested, same issue. I also implemented (delay 100); at the end of loop() as suggested and still did not fix the issue. Do you think the issue perhaps is on my end with my microcontroller and not the code/Arduino?

Hi,

I just retried uploading it and it worked both with and without the delay(100);!

I do not know what fixed the issue.

Thank you so much for all your help! Stay safe!

1 Like

You can tick the solution checkbox under your last post so people that might encounter the same problem know that a (maybe not very satisfing) solution was provided.

It's not "very satifying" because we don't know what was wrong. But at least you can continue.