First off, let me give a little bit of information about my situation and what I'm trying to accomplish with this project. I'm a mechanical engineering intern working to create a test fixture to evaluate prototype components for repeated use/longevity. The only coding experience I had before starting this project was with Matlab, so nothing as intensive as C/C++. The test fixture uses some solenoid-valves to operate four pneumatic linear actuators that press buttons or tilt components back and forth to simulate use. The solenoids are turned on and off using a Seeed Relay Shield V2 that I have attached to an Uno R3. So far I've developed enough of an understanding of coding to cobble together a sketch that can trigger all four relays at their predetermined cycling rates. Here it is:
// These variables store the flash pattern
// and the current state of the LED
int ledPin1 = 7; // the number of the LED pin
int ledState1 = LOW; // ledState used to set the LED
unsigned long previousMillis1 = -750; // will store last time LED was updated
long OnTime1 = 250; // milliseconds of on-time
long OffTime1 = 750; // milliseconds of off-time
int ledPin2 = 6; // the number of the LED pin
int ledState2 = LOW; // ledState used to set the LED
unsigned long previousMillis2 = -1000; // will store last time LED was updated
long OnTime2 = 1000; // milliseconds of on-time
long OffTime2 = 1000; // milliseconds of off-time
int ledPin3 = 5; // the number of the LED pin
int ledState3 = LOW; // ledState used to set the LED
unsigned long previousMillis3 = 0; // will store last time LED was updated
long OnTime3 = 1000; // milliseconds of on-time
long OffTime3 = 1000; // milliseconds of off-time
int ledPin4 = 4; // the number of the LED pin
int ledState4 = LOW; // ledState used to set the LED
unsigned long previousMillis4 = -990; // will store last time LED was updated
long OnTime4 = 10; // milliseconds of on-time
long OffTime4 = 1990; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
ledState1 = LOW; // Turn it off
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
}
else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
ledState1 = HIGH; // turn it on
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
}
if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
{
ledState2 = LOW; // Turn it off
previousMillis2 = currentMillis; // Remember the time
digitalWrite(ledPin2, ledState2); // Update the actual LED
}
else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
{
ledState2 = HIGH; // turn it on
previousMillis2 = currentMillis; // Remember the time
digitalWrite(ledPin2, ledState2); // Update the actual LED
}
if((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OnTime3))
{
ledState3 = LOW; // Turn it off
previousMillis3 = currentMillis; // Remember the time
digitalWrite(ledPin3, ledState3); // Update the actual LED
}
else if ((ledState3 == LOW) && (currentMillis - previousMillis3 >= OffTime3))
{
ledState3 = HIGH; // turn it on
previousMillis3 = currentMillis; // Remember the time
digitalWrite(ledPin3, ledState3); // Update the actual LED
}
if((ledState4 == HIGH) && (currentMillis - previousMillis4 >= OnTime4))
{
ledState4 = LOW; // Turn it off
previousMillis4 = currentMillis; // Remember the time
digitalWrite(ledPin4, ledState4); // Update the actual LED
}
else if ((ledState4 == LOW) && (currentMillis - previousMillis4 >= OffTime4))
{
ledState4 = HIGH; // turn it on
previousMillis4 = currentMillis; // Remember the time
digitalWrite(ledPin4, ledState4); // Update the actual LED
}
}
The reason the variables are named as led's is because I tested my code in 123D circuits using led's as visual analogues for the relays while I waited for my components to arrive in the mail. I've since tested the sketch on my actual components and it runs just fine.
Here is where things get interesting. In addition to actuating these buttons, the test fixture must also make sure that the equipment attached to the buttons actually functions when the buttons are pressed. In this case, the two buttons are attached to their own respective lights. What I am attempting to do is use two light sensors (Adafruit TSL2591's) to sense when each light is on or off. If the light is off when it is supposed to be on, or on when it is supposed to be off, the test fixture should be able to recognize this and stop the test, while also noting which test cycle the failure occurred at.
I have some ideas on how I think this could work, but as I said before, I have almost zero experience writing code so I'm at a loss on how to implement my ideas. Mainly I was thinking of finding benchmarks for light levels when the lights are on and off. The program can then check if the light level is above or below the levels and determine if the light is truly on or off. Using this information as a variable, I would then add a condition to my current code in my if statements that if the sensor does not match the expected state, to end the test and print what cycle the test was on. I based this idea on the examples provided by Adafruit on their website (Wiring & Test | Adafruit TSL2591 High Dynamic Range Digital Light Sensor | Adafruit Learning System) under the section titled "void getEvent(sensors_event_t*)".
My next issue is interpreting the data coming in from the TSL2591. So far I've only played around with the example sketch that provides a continually updating readout in the serial monitor. My goal is to have the program check the sensor value only when the if statement requires a reading to make a decision, but I don't know how to go about meshing the code for getting this reading with the code to create the output.
I wish I could be more specific in my questions, but as you can probably tell I'm in way over my head here with how little experience and knowledge of programming I have. Basically what I'd like to know is if my goals are realistically attainable, if what I have started so far makes sense considering the rest of the project, and what paths I should look down to get this completed. Any other suggestions for improvement or other methods of creating this program are both welcomed and appreciated. If there's any other information I left out that could help you to help me, please don't hesitate to ask. Thanks in advance for any help.