Coming from C#, How would u do an infinite loop?

Hello, i want to send some keys to my computer as a infinite loop, its for work purpose

lets say key "1"

i have arduino mini micro, i have c# background, i just need some tips

i have a button, i want to turn on and off the loop with this button, how my code should start or atleast where i should start

in c# its pretty easy, but this is all new to me. any help is welcome, thanks.

#define EVER ;;

...

...


for (EVER) {
  // Your infinite loop is right here
}

have a read thru Arduino buttons

n c# its pretty easy

As a matter of interest how would you do it in C# ?

UKHeliBob:
As a matter of interest how would you do it in C# ?

using while, and using a variable to turn if on and off

While(1) {
// Infinite loop here
}

That works even if you don't come from c#

while(1) {
// Infinite loop here
}

That works even if you don't come from c#

[Edit]

TheMemberFormerlyKnownAsAWOL:
It works if you spell it "while"

++Karma; // AWOL! Thank you for spotting my silly mistake :slight_smile:

PerryBebbington:

While(1) {

// Infinite loop here
}



That works even if you don't come from c#

It works if you spell it "while"

kokih:
using while, and using a variable to turn if on and off

Please post an actual example

The C# compiler will transform both

while (true) {
    // ...
}

and

for(;;) {
    // ...
}

into

{
  :label
    // ...
  goto label;
}

(as far as I remember)

Not far from C/C++

here is an example which loops

  1. waits for switch 1 to be pressed
  2. display message
  3. waits for switch 2 to be pressed
  4. displays message
void loop() 
{
    while(digitalRead(sw1Pin));
    Serial.println("Sw1 pressed ");
    while(digitalRead(sw2Pin));
    Serial.println("Sw2 pressed ");
 }

The Arduino system already has an infinite loop as standard. It is the loop() function.

...R

I'd make the empty loop much more obvious visually here:

    while(digitalRead(sw1Pin));
    Serial.println("Sw1 pressed ");

// is clearer like this:

    while(digitalRead(sw1Pin))
    {}
    Serial.println("Sw1 pressed ");

Its wise to make all the structure and indentation clear and visually consistent in code, you
may have to read it again some day! In this case a semicolon at the end of an if or a while
can cause huge confusion (witness many threads here!), so its best to always avoid doing it.

an alternative could be

    while(digitalRead(sw1Pin)) __asm__("nop");

Contributors to this Thread might like to reflect on the number of times that it has to be explained to newbies NOT to use blocking code such as WHILE, FOR and delay()

...R

Robin2:
Contributors to this Thread might like to reflect on the number of times that it has to be explained to newbies NOT to use blocking code such as WHILE, FOR and delay()

sure but that's not the question being asked :slight_smile:

And actually re-reading the ask, it's not an infinite loop that OP wants, it's really pseudo code like

while (button is pressed)
  send '1'

ideally we would let the loop spin and thus it would be

void loop()
{
 if (button is pressed) send '1'
}

which would be non blocking

J-M-L:
ideally we would let the loop spin and thus it would be

void loop()

{
if (button is pressed) send '1'
}


which would be non blocking

Exactly my point.

...R

something along the lines of

// blink LED when switch pressed
void loop() 
{
    if(!digitalRead(sw1Pin)) {
       digitalWrite(LED1, !digitalRead(LED1));
       delay(500);
       }
}

which inverts the LED when the switch is pressed

horace:
something along the lines of

// blink LED when switch pressed

void loop()
{
    if(!digitalRead(sw1Pin)) {
      digitalWrite(LED1, !digitalRead(LED1));
      delay(500);
      }
}



which inverts the LED when the switch is pressed

that's blocking the loop for half a second (forever in µc time) !

a non-blocking version?

// blink LED when switch pressed
void loop() 
{
    static unsigned long int time = millis();
    if(!digitalRead(sw1Pin) && (millis()-time > 500)) {
       digitalWrite(LED1, !digitalRead(LED1));
       time = millis();
       }
}

horace:
a non-blocking version?

// blink LED when switch pressed

void loop()
{
   static unsigned long int time = millis();
   if(!digitalRead(sw1Pin) && (millis()-time > 500)) {
      digitalWrite(LED1, !digitalRead(LED1));
      time = millis();
      }
}

Noooo....
You are setting time to millis() every time around loop, millis()-time will never be greater than 500.
Also, I suggest it is good practice to take this out of loop and put into it's own function instead off cluttering loop up with lots of code, which just gets confusing.

[Edit]
Ignore this!!! See posts below :slight_smile: