write data in an array in void loop

Hello all,

How can I write a variable in an array during the program is in void loop?

I wrote

long masque = valtoservo;[/quote] but it's not working.
Thank you in advance for your helping.

that's what I wrote.

 long i=0;

  while((i>=0) && (button ==1)){
    long masque[i] = valtoservo;
    i++;
  }
  1. It is not a void loop, it is a function called loop. Void is the variable type the function returns. By calling it this it marks you out as some one who knows little.

that's what I wrote.

So that writes the same value to all the elements in the loop and more. Or it does nothing. It depends on the value of the variable button which can not change once the loop starts. In fact once the while loop starts it will never stop and will write the variable value over every memory location.

  1. always post all your code.

that’s all the code:

#include<Servo.h>

Servo servo;

void setup() {
  Serial.begin(9600);
  servo.attach(9);
  pinMode(2,INPUT);
  pinMode(3,INPUT);

}

void loop() {
  
  int button = digitalRead(2);
  int button2 = digitalRead(3);

  int val  = analogRead(0);
  int valtoservo = map(val,0,1023,6,179);

  //Serial.print(val);



  long i=0;
  
  //Serial.println(button);
  
  while((i>=0) && (button ==1)){
    masque[i] = {valtoservo};
    i++;
    Serial.println(masque[i]);
  }

  if(button2 == 1){
    while(i>=0){
      servo.write(masque[i]);
      i++;
    }
 
  }
}

isn’t working :frowning:

isn't working

is only relevant if the code compiles. masque is NOT defined as an array.

    while((i>=0) && (button ==1)){
    masque[i] = {valtoservo};
    i++;
    Serial.println(masque[i]);
  }

This should probably either be an if statement or you should read the button again inside it. If button is 1 to get into this loop then it will overwrite a bunch of memory as it won't exit until i wraps back around to a negative number.

Still not working

#include<Servo.h>

Servo servo;
int masque[500];

void setup() {
  Serial.begin(9600);
  servo.attach(9);
  pinMode(2,INPUT);
  pinMode(3,INPUT);

}

void loop() {
  
  int button = digitalRead(2);
  int button2 = digitalRead(3);

  int val  = analogRead(0);
  int valtoservo = map(val,0,1023,6,179);

  //Serial.println(val);



  long i=0;
  
  Serial.println(button);
  
  while((i>=0) && (button ==1)){
    masque[i] = valtoservo;
    i++;
   // Serial.println(masque[i]);
    button = digitalRead(2);
    
  }

    while((i>=0) && (button2 == 1)){
      servo.write(masque[i]);
      i++;
      button2 = digitalRead(3);
     
    }
 
}

Still not working

Too damned bad.

Say what the code ACTUALLY does, and how that differs from what you expect.

Why do you want to make up to 500 copies of the same value? What happens why i hits 501?

 while((i>=0) && (button ==1)){
    masque[i] = valtoservo;
    i++;
   // Serial.println(masque[i]);
    button = digitalRead(2);
    
  }

You still have the same problem here. Once i is greater than 499 you are writing off the end of the array and clobbering the memory associated with something else. Who knows what effects that could have on your program. This probably shouldn't be a while loop. You probably should rely on the fact that the loop function repeats itself and let it record one value for each pass of loop.

Really PaulS question gets to the heart of the matter. Why do you want an array full of 500 copies of the same number over and over. What is the purpose of this program? What should it do.