You’re almost there. Have a look at this:
#define NUM_SOLENOIDS 5
const int solA = 1;
const int solB = 2;
const int solC = 3;
const int solD = 4;
const int solE = 5;
//
const int senA = 6;
const int senB = 7;
const int senC = 8;
const int senD = 9;
const int senE = 10;
int grSolenoids[] =
{
solA,
solB,
solC,
solD,
solE
};
int grSensors[] =
{
senA,
senB,
senC,
senD,
senE
};
void setup()
{
for( int i=0; i<NUM_SOLENOIDS; i++ )
{
pinMode( grSolenoids[i], OUTPUT );
pinMode( grSensors[i], INPUT_PULLUP );
}//for
}//setup
void loop()
{
for( int i=0; i<NUM_SOLENOIDS; i++ )
{
if( digitalRead( grSensors[i] ) == HIGH )
digitalWrite( grSolenoids[i], HIGH );
else
digitalWrite( grSolenoids[i], LOW );
}//for
}//loop
It just takes your code and cleans it up a bit and takes advantage of arrays to simplify things and reduce the amount of repetition.
If this works, you might then want to think about how it actually operates. The microcontroller repeats the loop() code thousands time times a second. There’s no need to update the state of the sensors and solenoids at that rate/
This modified version (should) update the sensors/solenoids once every 75mS; even that may be unreasonably rapid. You can change it to whatever you want:
#define NUM_SOLENOIDS 5
const int solA = 1;
const int solB = 2;
const int solC = 3;
const int solD = 4;
const int solE = 5;
//
const int senA = 6;
const int senB = 7;
const int senC = 8;
const int senD = 9;
const int senE = 10;
int grSolenoids[] =
{
solA,
solB,
solC,
solD,
solE
};
int grSensors[] =
{
senA,
senB,
senC,
senD,
senE
};
int
currSensorStates[NUM_SOLENOIDS],
lastSensorStates[NUM_SOLENOIDS];
void setup()
{
for( int i=0; i<NUM_SOLENOIDS; i++ )
{
pinMode( grSolenoids[i], OUTPUT );
pinMode( grSensors[i], INPUT_PULLUP );
//get a copy of the sensors "initial states"
lastSensorStates[i] = digitalRead( grSensors[i] );
}//for
}//setup
int ReadSensors( void )
{
static unsigned long
timeSensors = 0;
unsigned long
timeNow;
//update the sensors/solenoids once every 75mS (e.g.)
//get the systemmilliseconds count
timeNow = millis();
//if that count minus the previous time is less than 75(milliseconds),
//not enough time has passed yet so just return
//time has passed for
if( (timeNow - timeSensors) < 75 )
return;
//time for a new update; save this time for the next update
timeSensors = timeNow;
//run through the array of sensors and check them
for( int i=0; i<NUM_SOLENOIDS; i++ )
{
//read the current state of the sensor
currSensorStates[i] = digitalRead( grSensors[i] );
//is the state different than the last time checked?
if( currSensorStates[i] != lastSensorStates[i] )
{
//yes; apply the change to the solenoid
if( currSensorStates[i] == HIGH )
digitalWrite( grSolenoids[i], HIGH );
else
digitalWrite( grSolenoids[i], LOW );
}//if
//record the current reading into the "last" register for the next time through
lastSensorStates[i] = currSensorStates[i];
}//for
}//ReadSensors
void loop()
{
//just call this repeatedly; when it's time, ReadSensors will update the
//solenoids
ReadSensors();
}//loop
If you want to add time to the solenoid you’ll need to get a bit more complicated with a so-called “state machine.”