Array as shift register

I am trying to make a program. It is on another platform than Arduino, but the language is pretty similar to Arduino.

I have an conveyer, where products are incoming every 1 second.
I want to make an array of 100: array[0..99]
when a sensor is activated, the first bit of the array is activated, and this bit will cycle through the array, every 200ms.
then when product number 2 is activated, the first bit will be activated again, and will cycle through the array every 200ms.

In this case, only 2 bits are TRUE at the same time. (The code should let me do it for as many products as possible).

I have made a code, but here all Previous bits, that have been cycled through remain TRUE.

MY CODE example ( made in another language):


IF SRArrayOut AND TimerArray.Q THEN
FOR Index := 0 TO 99 BY 1 DO
arrShift[99 - Index + 1] := arrShift[99 - Index];
END_FOR
arrShift[0] := TRUE;
END_IF;

//SRArrayOUT : bool value for sensor
//TimerArray.Q : TimerOutput every 200ms


Hope u guys can help :slight_smile:

You are setting element 0 to true (1) after the shift, instead of 0.

Frankie777:
I am trying to make a program. It is on another platform than Arduino

Then please post your question on stackexchange or somewhere specific to your platform. This is an Arduino programming forum.

Frankie777:
MY CODE example ( made in another language):


IF SRArrayOut AND TimerArray.Q THEN
FOR Index := 0 TO 99 BY 1 DO
arrShift[99 - Index + 1] := arrShift[99 - Index];
END_FOR
arrShift[0] := TRUE;
END_IF;

//SRArrayOUT : bool value for sensor
//TimerArray.Q : TimerOutput every 200ms


Hope u guys can help :slight_smile:

would do it this way in Arduino environment/language:

#define ARR_SIZE 100
#define CYCLE_TIME 200 //200ms

#define SENSOR_PIN 2 //sensor outputs a digital signal. Also assuming Sensor goes high when triggered

bool bit_arr[ARR_SIZE];

uint8_t cnt_tail, cnt_head = 0;

unsigned long oldtime;

void setup() {
  pinMode(SENSOR_PIN, INPUT);
  oldtime = millis();
  cnt_tail = cnt_head;
}

void loop() {

  if (millis() - oldtime >= CYCLE_TIME) {
    bit_arr[cnt_head] = bit_arr[cnt_tail];
    cnt_tail = cnt_head;
    cnt_head = (cnt_head + 1) % ARR_SIZE; //circular buffer
  }

  if (digitalRead(SENSOR_PIN) == HIGH) bit_arr[0] = 1; //may want to add a flag or something to prevent
                                                         //unintentional re-triggers

}