Author Topic: Why does my while loop only run once in C code?  (Read 1287 times)

0 Members and 1 Guest are viewing this topic.

Offline DmeadsTopic starter

  • Regular Contributor
  • *
  • Posts: 171
  • Country: us
  • who needs deep learning when you have 555 timers
Why does my while loop only run once in C code?
« on: January 24, 2020, 06:47:48 am »
Hello!

New to C programming, and my while loop only runs once in the program. The uart displays the count correctlly, but the count wont restart or move to a different mode (the mode input is coming from an FPGA, this code for the xilinx ZYNQ SoC).

Code: [Select]

#include <stdio.h>
#include <xgpio.h>
#include "xparameters.h"
#include <sleep.h>
#include "platform.h"
#include "xil_printf.h"

#define DEFAULT 1

int main(void){


XGpio enable,LED;

int mode;
int countup;
int countdown;
int delay = 900000;

init_platform();
printf("counting up!\n\r");
XGpio_Initialize(&enable,XPAR_AXI_GPIO_0_DEVICE_ID);
XGpio_SetDataDirection(&enable, DEFAULT, 0xFFFF);

XGpio_Initialize(&LED,XPAR_AXI_GPIO_1_DEVICE_ID);
XGpio_SetDataDirection(&LED, DEFAULT, 0x0000);

while(1){

mode = XGpio_DiscreteRead(&enable, DEFAULT);

printf("mode %d", mode);

if (mode == 1){

print("counting up!\n\r");

for(countup = 0; countup <= 15; countup++){
XGpio_DiscreteWrite(&LED, DEFAULT, countup);
printf("count %d\n\r", countup);
usleep(delay);
if (countup == 16){
countup = 0;
}
}
}

else if (mode == 2){

print("counting down!\n\r");

for(countdown = 15; countdown >= 0; countdown--){
XGpio_DiscreteWrite(&LED, DEFAULT, countdown);
printf("count %d\n\r", countdown);
usleep(delay);

if (countdown == -1){
countdown = 15;
}
}
}

else{
XGpio_DiscreteWrite(&LED, DEFAULT, 0);
print("waiting for user input\n\r");
usleep(delay);
}

return 0;
cleanup_platform();
}
}


any thoughts?

Thanks
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2812
  • Country: nz
Re: Why does my while loop only run once in C code?
« Reply #1 on: January 24, 2020, 06:53:57 am »
Look carefully at which block of code there 'return' is in.

That would be my first guess, looking at it on my phone....
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 
The following users thanked this post: mcovington

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1801
  • Country: se
Re: Why does my while loop only run once in C code?
« Reply #2 on: January 24, 2020, 07:58:28 am »
What hamster_nz said.

Plus, there are other part s of code which might not do what you expect, if anything at all.

As a beginner in C, this is not surprising, but luckily many tools will help you detect and correct this kind of problems.
A good editor/IDE will automatically indent the code, in fact, it has (or you did) and the wrong position of the return statement is made clear by its indentation.
A good compiler, with the right options, will then warn you that the statement "cleanup_platform();" has no effect, as it will never be executed after the return.

Then there are the the if statement resetting the variables used for the loops: the condition in the the if will never be verified, so they are not needed.
Moving them outside the loop will cause the if to trigger, but with no practical effect (the variables are not used anywhere else, and are reset by the for statements).
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 9339
  • Country: fi
Re: Why does my while loop only run once in C code?
« Reply #3 on: January 24, 2020, 09:02:20 am »
A good compiler, with the right options, will then warn you

[emphasis added]

This; adding -Wall to the gcc command line catches so many beginner mistakes, and stupid mistakes even more experienced programmers do by accident. It's sad that most warnings are not enabled by default.
 
The following users thanked this post: newbrain, Dmeads

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2812
  • Country: nz
Re: Why does my while loop only run once in C code?
« Reply #4 on: January 24, 2020, 10:19:39 am »
You need a vastly smaller infinity of monkeys to type the works of Shakespeare if they have autocorrect enabled iPads.  ;D

Learning a new programming language by grabbing a smart IDE and then throwing the resulting code at a dialed-up compiler to catch gross structural errors all seems very post-millennial to me.

(But yes, get a good IDE and do turn on all the compiler warnings)
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 
The following users thanked this post: Dmeads

Offline emece67

  • Frequent Contributor
  • **
  • !
  • Posts: 614
  • Country: 00
Re: Why does my while loop only run once in C code?
« Reply #5 on: January 24, 2020, 04:10:09 pm »
.
« Last Edit: August 19, 2022, 03:43:41 pm by emece67 »
 
The following users thanked this post: Dmeads

Offline ghinckley68

  • Contributor
  • Posts: 12
  • Country: us
Re: Why does my while loop only run once in C code?
« Reply #6 on: February 02, 2020, 02:03:42 am »
Hello!

New to C programming, and my while loop only runs once in the program. The uart displays the count correctlly, but the count wont restart or move to a different mode (the mode input is coming from an FPGA, this code for the xilinx ZYNQ SoC).

Code: [Select]

#include <stdio.h>
#include <xgpio.h>
#include "xparameters.h"
#include <sleep.h>
#include "platform.h"
#include "xil_printf.h"

#define DEFAULT 1

int main(void){


XGpio enable,LED;

int mode;
int countup;
int countdown;
int delay = 900000;

init_platform();
printf("counting up!\n\r");
XGpio_Initialize(&enable,XPAR_AXI_GPIO_0_DEVICE_ID);
XGpio_SetDataDirection(&enable, DEFAULT, 0xFFFF);

XGpio_Initialize(&LED,XPAR_AXI_GPIO_1_DEVICE_ID);
XGpio_SetDataDirection(&LED, DEFAULT, 0x0000);

while(1){

mode = XGpio_DiscreteRead(&enable, DEFAULT);

printf("mode %d", mode);

if (mode == 1){

print("counting up!\n\r");

for(countup = 0; countup <= 15; countup++){
XGpio_DiscreteWrite(&LED, DEFAULT, countup);
printf("count %d\n\r", countup);
usleep(delay);
if (countup == 16){
countup = 0;
}
}
}

else if (mode == 2){

print("counting down!\n\r");

for(countdown = 15; countdown >= 0; countdown--){
XGpio_DiscreteWrite(&LED, DEFAULT, countdown);
printf("count %d\n\r", countdown);
usleep(delay);

if (countdown == -1){
countdown = 15;
}
}
}

else{
XGpio_DiscreteWrite(&LED, DEFAULT, 0);
print("waiting for user input\n\r");
usleep(delay);
}

return 0;
cleanup_platform();
}
}


any thoughts?

Thanks

ok get rid of return 0;  the return is the exit from the function. Same on most os and compilers not sure about the xlinx, but return  means exit from the function and return a 0 to the calling block of code, in this case that would probally be the xlixs, nothing else runs after the return.

 
The following users thanked this post: Dmeads


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf