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.