Automatising the pins

Hey there,

im working on a projekt and i want to automatise in wich rotation the pins go high. so for example i press a button -> arduino gets an signal set pin 22 to high than waits untill pin 23 get high and than sets pin 24 to high waits untill pin 25 send him its high ans so on. I tried to write a code wich makes that but it always stop after turning pin22 (the first pin) high.

thats the code on this area:

void loop() {
  buttonstate10 = digitalRead(pin10);
  if (buttonstate10 != lastbuttonstate10){
    if (buttonstate10 == HIGH){
      Serial.println("c" + String(digitalRead(pin10)));
      delay(5);
      }
  }
  lastbuttonstate10 = buttonstate10;
  if (Serial.available() > 0) {
    if (Serial.peek() == 'a'){
      int state = Serial.parseInt();
      digitalWrite(pin0, state);
      delay(5);
      val1 = digitalRead(pin10);
        digitalWrite(pin1, val1);}
        delay(5);

Some pieces of your program seem to be missing.

Paul

      Serial.println("c" + String(digitalRead(pin10)));

Don't do that. It pisses away resources like you would not believe.

Serial.write('c');
Serial.print(buttonstate10);
Serial.println();

Any time you have numbers in your variable names, you should probably be using arrays.

    if (Serial.peek() == 'a'){
      int state = Serial.parseInt();

If there is an 'a' in the Serial buffer then there's no integer that can be parsed. No decimal integer starts with 'a'. Learn to parse your own inputs instead of using the stupidly-limited parseInt().

Well thanks for your answers.

And yes i will clean my Code up after ist finished with Arrays. But right now im sill in the Phase were i only try to make a small part of it work. Later there will also be tempreture sensors and so on. also im pretty new to c++ so it might be wrong what im doing.

nevertheless i would like for any tipps how to get this lines to work:

void loop() {
  buttonstate10 = digitalRead(pin10);
  if (buttonstate10 != lastbuttonstate10){
    if (buttonstate10 == HIGH){
      Serial.println("c" + String(digitalRead(pin10)));
      delay(5);
      }
  }
  lastbuttonstate10 = buttonstate10;
  if (Serial.available() > 0) {
    if (Serial.peek() == 'a'){
      int state = Serial.parseInt();
      digitalWrite(pin0, state);
      delay(5);
      val1 = digitalRead(pin10);
        digitalWrite(pin1, val1);}
        delay(5);        
      }

and this is the whole Code:

int pin0 = 22;
int pin1 = 24;
int pin2 = 26;
int pin3 = 28;
int pin4 = 30;
int pin5 = 32;
int pin6 = 34;
int pin7 = 36;
int pin8 = 38;
int pin9 = 40;
int pin10 = 23;
int pin11 = 25;
int pin12 = 27;
int pin13 = 29;
int pin14 = 31;
int pin15 = 33;
int pin16 = 35;
int pin17 = 37;
int pin18 = 39;
int pin19 = 41;
int buttonstate10 = 0;
int lastbuttonstate10 = 0;
int val1 = 0;
int ledstate = 1;
void setup() {
 pinMode(pin1, OUTPUT);
 pinMode(pin2, OUTPUT);
 pinMode(pin3, OUTPUT);
 pinMode(pin4, OUTPUT);
 pinMode(pin5, OUTPUT);
 pinMode(pin6, OUTPUT);
 pinMode(pin7, OUTPUT);
 pinMode(pin8, OUTPUT);
 pinMode(pin9, OUTPUT);
 pinMode(pin0, OUTPUT);
 pinMode(pin11, INPUT);
 pinMode(pin12, INPUT);
 pinMode(pin13, INPUT);
 pinMode(pin14, INPUT);
 pinMode(pin15, INPUT);
 pinMode(pin16, INPUT);
 pinMode(pin17, INPUT);
 pinMode(pin18, INPUT);
 pinMode(pin19, INPUT);
 pinMode(pin10, INPUT);
 Serial.begin(9600);
}

void k0(){
  delayMicroseconds(35000);
  if (digitalRead(pin0) == LOW) {
    ledstate = digitalRead(pin1);
    digitalWrite(pin0, !ledstate);
  }
}
void k1(){
  delayMicroseconds(35000);
  if (digitalRead(pin1) == LOW) {
    ledstate = digitalRead(pin2);
    digitalWrite(pin1, !ledstate);
  }
}
void k2(){
  delayMicroseconds(35000);
  if (digitalRead(pin2) == LOW) {
    ledstate = digitalRead(pin1);
    digitalWrite(pin2, !ledstate);
  }
}
void k3(){
  delayMicroseconds(35000);
  if (digitalRead(pin3) == LOW) {
    ledstate = digitalRead(pin2);
    digitalWrite(pin3, !ledstate);
  }
}
void k4(){
  delayMicroseconds(35000);
  if (digitalRead(pin4) == LOW) {
    ledstate = digitalRead(pin1);
    digitalWrite(pin4, !ledstate);
  }
}
void k5(){
  delayMicroseconds(35000);
  if (digitalRead(pin5) == LOW) {
    ledstate = digitalRead(pin2);
    digitalWrite(pin5, !ledstate);
  }
}
void k7(){
  delayMicroseconds(35000);
  if (digitalRead(pin7) == LOW) {
    ledstate = digitalRead(pin1);
    digitalWrite(pin7, !ledstate);
  }
}
void k6(){
  delayMicroseconds(35000);
  if (digitalRead(pin6) == LOW) {
    ledstate = digitalRead(pin2);
    digitalWrite(pin6, !ledstate);
  }
}
void k8(){
  delayMicroseconds(35000);
  if (digitalRead(pin8) == LOW) {
    ledstate = digitalRead(pin1);
    digitalWrite(pin8, !ledstate);
  }
}
void k9(){
  delayMicroseconds(35000);
  if (digitalRead(pin9) == LOW) {
    ledstate = digitalRead(pin2);
    digitalWrite(pin9, !ledstate);
  }
}

void loop() {
  buttonstate10 = digitalRead(pin10);
  if (buttonstate10 != lastbuttonstate10){
    if (buttonstate10 == HIGH){
      Serial.println("c" + String(digitalRead(pin10)));
      delay(5);
      }
  }
  lastbuttonstate10 = buttonstate10;
  if (Serial.available() > 0) {
    if (Serial.peek() == 'a'){
      int state = Serial.parseInt();
      digitalWrite(pin0, state);
      delay(5);
      val1 = digitalRead(pin10);
        digitalWrite(pin1, val1);}
        delay(5);        
      }
    if (Serial.peek() == 'c'){
      int state = Serial.parseInt();
      digitalWrite(pin0, state);
    }
    if (Serial.peek() == 'd') {
      int state = Serial.parseInt();
      digitalWrite(pin1, state);
    }
    if (Serial.peek() == 'e'){
      int state = Serial.parseInt();
      digitalWrite(pin2, state);
    }
    if (Serial.peek() == 'f') {
      int state = Serial.parseInt();
      digitalWrite(pin3, state);
    }
    if (Serial.peek() == 'g'){
      int state = Serial.parseInt();
      digitalWrite(pin4, state);
    }
    if (Serial.peek() == 'h') {
      int state = Serial.parseInt();
      digitalWrite(pin5, state);
    }
    if (Serial.peek() == 'i'){
      int state = Serial.parseInt();
      digitalWrite(pin6, state);
    }
    if (Serial.peek() == 'j') {
      int state = Serial.parseInt();
      digitalWrite(pin7, state);
    }if (Serial.peek() == 'k'){
      int state = Serial.parseInt();
      digitalWrite(pin8, state);
    }
    if (Serial.peek() == 'l') {
      int state = Serial.parseInt();
      digitalWrite(pin9, state);
    }
while (Serial.available() > 0){
    Serial.read();
  }
}

use your arrays!!!!

const uint8_t outputPins[] = {22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
const uint8_t inputPins[]  = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41};

int buttonstate10 = 0;
int lastbuttonstate10 = 0;
int val1 = 0;
int ledstate = 1;

void setup() 
{
  for (auto i : outputPins)
    pinMode(i, OUTPUT);
  for (auto i: inputPins)
    pinMode(i, INPUT);
  Serial.begin(9600);
}

Ok well @BulldogLowell, that's going to need some elaboration.

It's intuitive what this does:

(auto i : outputPins)

....but by no means how. Obviously a short hand for the normal "for", and seems to do an increment by 1 but is that some default or what, and how does it know when to stop? Presumably at the end of the array, but how does it know it's an array in the first place?

(Yes, I did ask Dr Google but all the explanations I found on "auto" are very deep and in any case didn't specifically address this use. )

wilfredmedlin: Ok well @BulldogLowell, that's going to need some elaboration.

It's intuitive what this does:

(auto i : outputPins)

....but by no means how. Obviously a short hand for the normal "for", and seems to do an increment by 1 but is that some default or what, and how does it know when to stop? Presumably at the end of the array, but how does it know it's an array in the first place?

(Yes, I did ask Dr Google but all the explanations I found on "auto" are very deep and in any case didn't specifically address this use. )

google: C++ Ranged Based For Loop

BulldogLowell: google: C++ Ranged Based For Loop

Pretty much what I had found before, and it's all written for the hardened c++'er.

But nvm, I get the gist.

Question though: I expected i to be the index to the array, but it's evidently that actual content.

Otherwise this:

pinMode(i, OUTPUT);

.... would need to be this:

pinMode(outputPins[i], OUTPUT);

Have I got that right?

And then if I did want to use the index, say just to print the index not the content, how would I do that?

I reckon for a newbie a simple FOR loop is easier to understand than having to go to the trouble of looking up and learning about C++ Ranged Based For Loop

It's not as if using the ranged base loop makes the program work any better.

...R

wilfredmedlin:
…Have I got that right?..

And then if I did want to use the index, say just to print the index not the content, how would I do that?

So, it uses an iterator (google ranged based for loop iterator) not an index so this:

for (auto i : outputPins)
{
 Serial.println(i);
}

prints values… there is no way to print an “index” because it is not there.

if you wanted to use a range based for loop that does this for example:

for (size_t i = 0, i < sizeof(myArray)/sizeof(myArray[0]), i++)
{
  myArray[i] = i;
  Serial.println(myArray[i]);
}

you would have to do something like this:

int i = 0;
for (auto& j : myArray)
{
  j = i++;
  Serial.println(j);
}

it’s syntactic sugar (compiles to the same size as it’s sister, the for loop) meant to make things like this:

for (size_t i = 0, i < sizeof(myArray)/sizeof(myArray[0]), i++)

look like this:

for (auto& i : myArray)

or like this:

for(std::vector<Brazos*>::iterator iter = instanceAddress.begin(); iter !=instanceAddress.end(); iter++)

look like this:

for(auto& iter = instanceAddress)

much easier to type and easy to understand… it iterates over all the elements of an array or any other container (e.g. vector).

Robin2:
I reckon for a newbie a simple FOR loop is easier to understand than having to go to the trouble of looking up and learning about C++ Ranged Based For Loop

it is certainly arguable that a typical index=based for loop is easier than “iterate over the contents of X”

well, one has to go to the trouble of learning everything (lest they live in ignorance) I suppose; including learning the basic for loop.

Robin2: I reckon for a newbie a simple FOR loop is easier to understand than having to go to the trouble of looking up and learning about C++ Ranged Based For Loop

It's not as if using the ranged base loop makes the program work any better.

...R

+1