New to C and Arduino - How could I clean up my code?

I just made a small thing that lights up LED's in an order then loops. The problem is I know I could make

the code neater, I do programming in lua(Although I do game programming with it in LOVE2D, so all this

hardware stuff is new to me) so I know that it SHOULD be neater and more efficient, but I cant quite

grasp how. Probably with a for loop in there? I'm not sure, could someone maybe lend me a hand?

bool O = true;
bool R = false;

void setup() {
    pinMode(9, OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(7, OUTPUT);
}

void loop() {
    if (O == true)
    {
        analogWrite(9, 255);
        delay(1000);
        analogWrite(9, 0);
        O = false;
        R = true;
    }
    if (O == false)
    {
        if (R == true)
        {
            analogWrite(8, 255);
            delay(1000);
            analogWrite(8, 0);
         R = false;
        }
    }
    
     if (O == false)
    {
        if (R == false)
        {
            analogWrite(10, 255);
            delay(1000);
            analogWrite(10, 0);
         O = true;
        }
    }
}

Lewn1e:
I just made a small thing that lights up LED's in an order then loops. The problem is I know I could make

the code neater, I do programming in lua(Although I do game programming with it in LOVE2D, so all this

hardware stuff is new to me) so I know that it SHOULD be neater and more efficient, but I cant quite

grasp how. Probably with a for loop in there? I'm not sure, could someone maybe lend me a hand?

A very good place to start would be to add some comments describing what the code is doing (or should be doing). A comment can start with double slashes "//" and continues to the end of the line, or it can start with slash-asterisk "/" and will continue until the next asterisk-slash "/" even if it's several lines later.

// This is a single line comment.

int i;  // You can put them after a statement
// but not before!  int j;
// The compiler will never see that "int j;" declaration.

/*  You can also make
     multiline
     comments */

int k; /* you can use this style of comment on a single line too */

BobKay:
A very good place to start would be to add some comments describing what the code is doing (or should be doing). A comment can start with double slashes "//" and continues to the end of the line, or it can start with slash-asterisk "/" and will continue until the next asterisk-slash "/" even if it's several lines later.

// This is a single line comment.

int i;  // You can put them after a statement
// but not before!  int j;
// The compiler will never see that "int j;" declaration.

/*  You can also make
    multiline
    comments */

int k; /* you can use this style of comment on a single line too */

I'm aware of comments, although ive never gotten into the habit of using them. You're right that I should be commenting, especially if im going to be posting the code online and asking for help haha.

That's not quite what I mean though, i was thinking something more like making the code more efficient/condensed

Lewn1e:
I'm aware of comments, although ive never gotten into the habit of using them. You're right that I should be commenting, especially if im going to be posting the code online and asking for help haha.

That's not quite what I mean though, i was thinking something more like making the code more efficient/condensed

Well, I started looking over the code to see if I might be able to offer a suggestion or two, but I really don't know what it's trying to do. As I read the code, the O and R variables and all the conditionals testing them don't matter in the least. It'll do the same thing if it were simply:

void loop( )
{
    analogWrite(9, 255);
    delay(1000);
    analogWrite(9, 0);

    analogWrite(8, 255);
    delay(1000);
    analogWrite(8,0);

    analogWrite(10,255);
    delay(1000);
    analogWrite(10,0);
}

So my thinking is you must have had something in mind that I'm just not seeing. And that's when I suggested commenting it.

Incidentally you set pin 7 as OUTPUT then never use it, and you don't set pin 10 to anything before using it. Perhaps you changed the pin in part of the code from 7 to 10 or vice versa but not in the rest of the code?

There's also the fact that pin 8 is not a PWM pin on some arduinos (like the Uno) although it is on others (like the Mega 2560).

Avoid use of delay(). In your current code it works but in general writing blocking code is very limiting. Every time I've started with blocking code thinking it would be fine I ended up having to do a major rewrite later when I wanted to add a feature that was not compatible with the use of delay(). I recommend studying Examples > 02.Digital > BlinkWithoutDelay and the associated tutorial page:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.

If you're only using analogWrite() values of 255 or 0 then do digitalWrite() of HIGH or LOW instead. analogWrite() should only be used if you want to dim the LEDs using analogWrite() values other than 0 and 255.

Use descriptive variable names, especially if you don't like writing comments.

Avoid the use of "magic numbers". These make your code harder to understand and more difficult to maintain. Examples of magic numbers in your code are the pin numbers and the delay values. You could write it without magic numbers like so:

const byte led1pin = 9;
const byte led2pin = 8;
const byte led3pin = 7;

const unsigned int delayDuration = 1000;

bool O = true;
bool R = false;

void setup() {
    pinMode(led1pin, OUTPUT);
    pinMode(led2pin,OUTPUT);
    pinMode(led3pin, OUTPUT);
}

void loop() {
    if (O == true)
    {
        analogWrite(led1pin, 255);
        delay(delayDuration);
        analogWrite(led1pin, 0);
        O = false;
        R = true;
    }
    if (O == false)
    {
        if (R == true)
        {
            analogWrite(led2pin, 255);
            delay(delayDuration);
            analogWrite(led2pin, 0);
         R = false;
        }
    }
   
     if (O == false)
    {
        if (R == false)
        {
            analogWrite(led3pin, 255);
            delay(delayDuration);
            analogWrite(led3pin, 0);
         O = true;
        }
    }
}

Note that this would have prevented you from making the error BobKay noted where you are using 10 for the pin number of your third LED in some parts of your code and 7 in other parts. Removing the magic numbers means you can easily change the configuration by only editing a single line. This also makes it easier to understand the intent of the code even without comments.

The names I used for the led pins could be much more descriptive if I had more knowledge of your project.

Consider putting your LED pin values in an array:

const byte ledPins[] = {9, 8, 7};

This could allow you make your code easily expandable to any number of LEDs you like by simply adding their pin definitions to the array, for example:

for (byte pinCounter = 0; pinCounter < sizeof(ledPins) / sizeof(ledPins[0]); ++pinCounter)
{
    pinMode(ledPins[pinCounter], OUTPUT);
}

You may say this code is actually more complex and larger than what you had but if you add more LEDs it will become more efficient. Putting the LED pins in an array could greatly reduce the amount of code in your loop().

I was going to say do an Auto Format but it looks like that feature doesn't exist in Create Editor (EDIT: this thread was originally posted in the Create Editor forum section). That's really a shame because it's an excellent troubleshooting tool.

@ Pert

You could add it to the wishlist thread.
Am sure your voice would add a bit of weight.