Array with large number of elements

const byte tap = 2;
signed long dots[] = 
{1, 2, 3, 4, 
5, 6, 7, 8, 
9, 10, 11, 12, 
13, 14, 15, 16, 
17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32,
33, 34, 35, 36,
37, 38, 39, 40,
41, 42, 43, 44,
45, 46, 47, 48};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(tap, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  
{
  if(!digitalRead(tap))
  {
  Serial.print(dots[22]);
  Serial.print(" ");
  while(!digitalRead(tap));
  delay(500);
  }
}

}

Just like above example
For a very huge amount of array elements say 50 or 250 or 400
Is there anything we can do it to define without writing all the numbers from 0 to 100 ???

Do you need the array if the data is from 1 to 100 ? will the data change ?

define the array and fill it in in the set up.

const size_t nbEntries = 100;
signed long dots[nbEntries];

void setup() {
  for (signed long i=0; i< nbEntries; i++) dots[i] = i+1; // remember arrays index starts at 0
}

Of course it is:

#define N 400
signed long dots [N];
for (signed long i = 0; i < N; i++)
    dots [i] = i;

But why put a sequence of increasing numbers in the array in the first place? You already know what you'll find there anyway.

2 Likes

a for loop will do the job

for (int i = 0; i < 100; i++)
{
  dots[i] = i;
}

or slightly more complex content

for (int i = 0; i < 100; i++)
{
  dots[i] = i * 5;  //  or i*i whatever formula is needed
}

Only if the numbers can be calculated. In your case it looks like you are just an increment. So you have no need to put them into an array at all.

When you want a specific number just use it.

For example

Can be written as

 Serial.print(22);
1 Like

needs i+1 as his array starts with 1 at index 0

1 Like

that was left as hidden exercise :wink:

1 Like

:slight_smile:

Damned I ruined it

OK I tried this one from your script

const byte tap = 2;

int i = 1;

const size_t nbEntries = 100;
signed long dots[nbEntries];

void setup() {
  Serial.begin(9600);
  pinMode(tap, INPUT_PULLUP);
}

void loop(){
  
  if(!digitalRead(tap))
  {
      for (signed long i=0; i< nbEntries; i++) 
      {
        Serial.print(i+1);
        while(!digitalRead(tap));
      }
    while(!digitalRead(tap));
  }
}

Ya that is working but here I need to print the increment as per the button operation

Here when I pressed the button it started printing form 1 to 100 and stopped

Yes it is due to the for loop which automatically prints the defined range on a single press of push button

I want it to print 1 2 3 4 5 6 according to each time the button press.

Please guide !!

yeah depending on your code I designed this one

const byte tap = 2;

#define N 400
signed long dots [N];

void setup() {
  Serial.begin(9600);
  pinMode(tap, INPUT_PULLUP);
}

void loop(){
  
  if(!digitalRead(tap))
  {

    for (signed long i = 0; i < N; i++)
      {
      dots [i] = i;
      Serial.print(dots [i]);
      }
	while(!digitalRead(tap));
  }
}

Now on a single press it starts printing from 0 to 399 at once (due to for loop)

But I need this increment step wise one by one depending on the each time of button press.

Please guide

Ya I tried this type of thing but want to make a step wise movement one by one of numbers depending on each button press

For Loop is doing auto increment but I want to control it with push button

Take a look at the StateChangeDetection sketch in the IDE examples.
File>Examples>02Digital

1 Like

Yeah I just checked it is there

Need to work on it . . . ll be back after that !!

yeah actually let me clarify the thing here

I am applying this on for a digital lock code like this

const byte Trig1 = 22;

const byte Led1 = 7;
const byte Led2 = 8;
const byte Led3 = 9;
const byte Led4 = 10;

signed long count = 1;

const byte led0 ;

signed long LEDpins[] = {led0, Led1, Led2, Led3, Led4};

signed long dots[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

void setup() {
  Serial.begin(9600);

  pinMode(Trig1, INPUT_PULLUP);

  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);
}

void loop() {

  // Pro(1, Trig1, &LEDpins[1]);         // Not Working
  Pro(&dots[1], Trig1, &LEDpins[3]);    //  Working

}


void Pro(signed long * dotter, const byte * TRIGs, signed long * myPins)
{

if(count == * dotter)

  {

  if(!digitalRead(TRIGs))
  {
    digitalWrite(* myPins, HIGH);
    while(!digitalRead(TRIGs));
  }
  else
  {
    digitalWrite(* myPins,LOW);
  }

  }

}

Now in this above code you can check it out as mentioned

We have here

  // Pro(1, Trig1, &LEDpins[1]);         // Not Working
  Pro(&dots[1], Trig1, &LEDpins[3]);    //  Working

within the void loop

The reason for the ARRAY thing is due to the proper working of this line

Pro(&dots[1], Trig1, &LEDpins[3]);

While this line with a simple numeric entry is not working any way

Pro(1, Trig1, &LEDpins[1]);

Actually the working is like :-
For this secret lock a numeric value between 1 to 400 ll be provided to the user
and that numeric value would be placed in the code in a simple way as '1' or anything
like that OR the value would be placed inside the bracket of "&dots[1]".

Weekly or daily the numeric ll be placed and ll be allotted to the User.

That is the aim actually

There could be multiple such set of locks for a combination to open the gate
so here in the code I applied the void argument to make the script short and I am facing problem with this count value :frowning:

Please guide !!!

So don't get it.
What exactly are you saying here?

If you can't calculate the array values as a continuation of the sequence then what do you want to put in the higher values. Is it random numbers or just an offset in your algorithm?

Stone Soup - All that is needed is an array of incrementing numbers.
Stone Soup is better when you add buttons, and that is all.
Stone soup is even better when adding incrementing buttons as a final ingredient
Stone soup is best when adding a digital lock, LEDs, et c.
Stone soup is better than best with multiple combination locks.
Stone soup is even better than best if combinations change weekly, or daily...
Stone soup... is best.

Hey.. where is the help?

1 Like

Why exactly does the Pro function need a pointer to a signed long, instead of passing the signed long itself?

Just check with this very fresh code

const byte Trig1 = 22;

const byte inc = 4;
const byte dec = 5;

const byte Led1 = 7;
const byte Led2 = 8;
const byte Led3 = 9;
const byte Led4 = 10;

signed long count = 1;

signed long level1 = 1;

const byte led0 ;

signed long LEDpins[] = {led0, Led1, Led2, Led3, Led4};

signed long dots[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

signed long bots[50];

int pack1 = 400;

void setup() {
  Serial.begin(9600);

  pinMode(Trig1, INPUT_PULLUP);
  pinMode(inc, INPUT_PULLUP);
  pinMode(dec, INPUT_PULLUP);

  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);

  Serial.print(level1);

  
}

void loop() {

  INCDEC(&pack1);

  Pro(Trig1, &LEDpins[3], &dots[1]);    //  Working
  Pro(Trig1, &LEDpins[4], &dots[2]);    //  Working
  Pro(Trig1, &LEDpins[2], &dots[3]);    //  Working
  Pro(Trig1, &LEDpins[1], &dots[4]);    //  Working

}


void Pro(const byte * TRIGs, signed long * myPins, signed long * dotter)
{

if(count == * dotter)

  {

  if(!digitalRead(TRIGs))
  {
    digitalWrite(* myPins, HIGH);
    while(!digitalRead(TRIGs));
  }
  else
  {
    digitalWrite(* myPins,LOW);
  }

  }

}

void INCDEC(int * jar)
{
  if(!digitalRead(inc))
  {
    if (count<* jar)
    {
      count++;
      delay(250);
      Serial.print(count);
    }
  }
  if(!digitalRead(dec))
  {
    if (count>1)
    {
      count--;
      delay(250);
      Serial.print(count);
    }
  }
}

You ll get everything here

yes randomly I want to set any of the output as a data out to the further unit in this way using up down counter and choosing a value between 1 - 400 to set it

The range may be any like 1- 800 or 900 but it ll be confidential

And here the 4 LEDs I added is just for the example as I can have multiple outs like 4 to 16 or 2 to 8 multiplexers etc things

Well that is not the concern

Imp thing is to put the value between the given range without carrying arrays of huge amount of elements

That is the only purpose to solve !!

Dint get this point sir . . would you please elaborate a bit??

Why do you want the Pro function to have arguments that are pointers, instead of just passing the values directly?