Turn on several digital outputs at the same time

Hi everyone, thi is the challenge/question. (No PORTX registers must be used)

The thing is to use for sentence to make it work, you can use if and for, but nothing else but the digitalWrite to activate the outputs.

--
Secuence: (you can use from pin 0-7 or 2-9 if you use Arduino Uno)

Pin 0=On delay(1000) then Pin 0=Off delay(500)

Pin 0 and 1= On delay(1000) thenPin 0 and 1=Off delay(500)

Pin 0 1 and 2 = On delay(1000) then Pin 0 1 and 2= Off delay(500)

.... and so on till' you get to pin 7 and then they must shut down...

I have this, but it doesn't work quite right....

int t1(200), t2(100), t3(500);
void setup() {  
     for ( int i=0;i<8; i++){
       pinMode(i,OUTPUT); 
                            }                  
}
void loop() {
                for(int i=0;i<1;i++) {
                digitalWrite(i,HIGH); delay(t1);
                digitalWrite(i,LOW); delay(t1);
                                     }
                   
                   for(int i=0;i<1;i++){
                 if (i > i++) digitalWrite(i, HIGH);
                                       }
}

Or this, this is even worse...

void loop() {
                for(int i=0;i<1;i++) {
                digitalWrite(i,HIGH); delay(t1);
                digitalWrite(i,LOW); delay(t1);
                                     }
                   
                   for(int i=0;i<2;i++){
                  digitalWrite(i,HIGH); delay(t1);
                                       }
}
int t1(200), t2(100), t3(500);

What do you mean it doesn't work right? This won't even compile.

Please read the sticky thread at the top of the board. Specifically the one titled "How to Use This Forum" before posting anything else. Use code tags when posting code.

                 for(int i=0;i<1;i++) {
                 digitalWrite(i,HIGH); delay(t1);
                 digitalWrite(i,LOW); delay(t1);
                                      }

What is the point of a for statement (NOT sentence) that you KNOW will only iterate once?

for(int i=0;i<1;i++){
                  if (i > i++) digitalWrite(i, HIGH);
                                        }

For what values of i, in the range 0 to 0, will i be greater than i+1?

Delta_G:

int t1(200), t2(100), t3(500);

What do you mean it doesn't work right? This won't even compile.

It's stupid looking, but it IS valid:

Binary sketch size: 1,106 bytes (of a 30,720 byte maximum)

If, as your title says, you want to "Turn on several digital outputs at the same time" why on earth have you got delay()s in your code

for(int i=0;i<2;i++){
   digitalWrite(i,HIGH); delay(t1);
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Please do not put multiple statements on the same line - it just makes code hard to read.

...R

Delta_G:

int t1(200), t2(100), t3(500);

What do you mean it doesn't work right? This won't even compile.

Please read the sticky thread at the top of the board. Specifically the one titled "How to Use This Forum" before posting anything else. Use code tags when posting code.

It does compile, it is another way to write it, even if it's not the traditional....

PaulS:

for(int i=0;i<1;i++){

if (i > i++) digitalWrite(i, HIGH);
                                        }



For what values of i, in the range 0 to 0, will i be greater than i+1?

It's actually post-increment, so it might even be undefined! I don't feel like deciphering the sequencing rules right now to try and figure out what it'll actually do. It might check if i is greater than itself, or if i+1 is greater than itself.

Either way, this is the kind of code that makes you think "What do you even want this to do?".

It's actually post-increment, so it might even be undefined!

The relational statement is indeed undefined, but the code would do something.

It is anyone's guess what that something might be!

Delta_G:

int t1(200), t2(100), t3(500);

What do you mean it doesn't work right? This won't even compile.

This is C++, not C.

Hi,

What your teacher wants you to realise is that you can use variables in a for loop.

Jacques.

Here is an interesting surprise!

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("starting");
int i=1;
Serial.print( "1. (i > i) ");
Serial.println( (i > i) );
Serial.print( "2. (i > i++) ");
Serial.println( (i > i++) );
Serial.print("i = ");
Serial.println(i);
}

void loop() {
  // put your main code here, to run repeatedly:
}

The results of 1. and 2. are 0 and 1, respectively.

So, the Arduino, aided and abetted by gcc, seems to think that "i" is greater than "i++" !

Second hint:

This is perfectly legal:

for (int i = 0 ; i < aVariable ; i++) {
  //Do someting
}

jremington:
So, the Arduino, aided and abetted by gcc, seems to think that "i" is greater than "i++" !

Those that use side effects in expressions need to understand the rules! "i > i++" is undefined.

Those that use side effects in expressions need to understand the rules! "i > i++" is undefined.

I don't think that Brian was trying to use side effects to some advantage. He is a newbie. I don't want to know what went thru his mind when he came up with this construct. But he already knows that his code does not work and asked for help.