change sensors after period of time

we have a center pivot irrigation model and were trying to change moisture sensors in a circle by using the number of revolutions from a motor. We have 6 sensors in 3 different sections with two sensors in each section. Each sensor is controlling flashing LEDs and we need to switch between sensors/sections as it travels around the circle.

here is are code so far we. we need to get the sensors to switch and it will read that sensor based on the section that it is in.

int led1 = 2;
int led2 = 3;

int val;
int motorpin = 9;
int encoderPinA = 6;
int encoderPinB = 7;
int encoderPos = 0;
int encoderPinALast = LOW;
int n = LOW;

unsigned long led1timer = 0;
unsigned long led2timer = 0;
int led1State = LOW;
int led2State = LOW;

int sensor1 = 0;
int sensor2 = 1;
int sensor3 = 2;
int sensor4 = 3;
int sensor5 = 4;
int sensor6 = 5;

void setup(){
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);

pinMode(motorpin, OUTPUT);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
Serial.begin(9600);
}

void loop(){

int MC1 = analogRead(sensor1);
int MC2 = analogRead(sensor2);
int MC3 = analogRead(sensor3);
int MC4 = analogRead(sensor4);
int MC5 = analogRead(sensor5);
int MC6 = analogRead(sensor6);

int delaytime1 = (50 + 950/700 * MC1);
int delaytime2 = (50 + 950/700 * MC2);
int delaytime3 = (50 + 950/700 * MC3);
int delaytime4 = (50 + 950/700 * MC4);
int delaytime5 = (50 + 950/700 * MC5);
int delaytime6 = (50 + 950/700 * MC6);

analogWrite(motorpin, 100);

n = digitalRead(encoderPinA);
if((encoderPinALast == LOW) && (n == HIGH)){
if(digitalRead(encoderPinB) == LOW)
{
encoderPos--;
}
else{
encoderPos++;
}
Serial.println(encoderPos/180);

}
encoderPinALast = n;

if((encoderPos> 0) && (encoderPos< 25))
{
(analogRead(sensor1));
digitalWrite(led1, delaytime1);
(analogRead(sensor2));
digitalWrite(led2, delaytime2);
}

if((encoderPos> 25) && (encoderPos< 50))
{
(analogRead(sensor3));
digitalWrite(led1, delaytime3);
(analogRead(sensor4));
digitalWrite(led2, delaytime4);
}
if ((encoderPos> 50) && (encoderPos <75))
{
(analogRead(sensor5));
digitalWrite(led1, delaytime5);
(analogRead(sensor6));
digitalWrite(led2, delaytime6);
}

if ( (millis () - led1timer) >= delaytime1) {
if (led1State == LOW)
led1State = HIGH;
else
led1State = LOW;
digitalWrite (led1, led1State );
led1timer = millis ();
}
if ( (millis () - led2timer) >= delaytime2 ) {
if (led2State == LOW)
led2State = HIGH;
else
led2State = LOW;
digitalWrite (led2, led2State );
led2timer = millis () ;
}
if ( (millis () - led1timer) >= delaytime3) {
if (led1State == LOW)
led1State = HIGH;
else
led1State = LOW;
digitalWrite (led1, led1State );
led1timer = millis ();
}
if ( (millis () - led2timer) >= delaytime4 ) {
if (led2State == LOW)
led2State = HIGH;
else
led2State = LOW;
digitalWrite (led2, led2State );
led2timer = millis () ;
// }
// if ( (millis () - led1timer) >= delaytime5) {
// if (led1State == LOW)
// led1State = HIGH;
// else
// led1State = LOW;
// digitalWrite (led1, led1State );
// led1timer = millis ();
// }
// if ( (millis () - led2timer) >= delaytime6 ) {
// if (led2State == LOW)
// led2State = HIGH;
// else
// led2State = LOW;
// digitalWrite (led2, led2State );
// led2timer = millis () ;
// }

}
}

Can you make a drawing, to explain it ?
There is an encoder attached to the motor ? and that tells the position ? what is the delaytime for ?

You need to normalise the encoder position so that it stays in the range between zero and however many pulses make up a complete revolution. Assuming the encoder is arranged so that the normal direction of rotation increments the position, you can do that at the point of incrementing it with the modulus operator like this:

encoderPos = (encoderPos+1) % PULSES_PER_REVOLUTION;

Then you can work out which sector it is in by division:

sector = encoderPos / PULSES_PER_SECTOR;

Then you can use sector as the index into your array of sensor pairs, or whatever else you want to do with it.

We're using an encoder

Nothing seems to happen when the encoder value = 50? Perhaps it doesn't matter?

There is a lot of repetition in the code. How about using arrays?

If the encoder always increments the tests can be greatly simplified. If it has already passed 25 there is no need to check for that again when it exceeds 50.

...R

we have it set at 25 revolutions per section and its supposed to switch sensors at 50 and 75 and so on in a continuous loop.

could we get an example of the code we need for the array

It's hard to know where to start.

For example

byte sensors[6] = {0, 1, 2, 3, 4, 5}; 
int MCs[6];
byte delayTimes[6];

for (byte n = 0; n < 6; n++) }{
  MCs[n] = analogRead(sensors[n]);
  delayTimes[n] = (50 + 950/700 * MCs[n]);
}

I think you could also simplify the decision making code with something like this

if (encoderPos > maxEncoderPos) {
  encoderPos = 0;
}

if (encoderPos > 75) {
   // stuff for that segment
}
else if (encoderPos > 50) {
  // stuff for that segment
}
else if (encoderPos > 25) {
  // stuff for that segment
}
else { // encoder pos >= 0
  // stuff for that segment
}

...R