Hi everyone.
My apologise if I have posted in the wrong forum. I'm new to coding and have a problem with my code.
I am trying to write the code to cycle on and off a solenoid to test the endurance (number of cycles) before it fails. The solenoid has a plunger which pushes a button when the solenoid has power supplied to it and the plunger retracts when power to the solenoid is removed. The button which the plunger pushes onto is intended as a feedback which I read into the arduino as a digital input.
The button feedback is used to determine whether the solenoid plunger has moved or not.
If the solenoid is powered (plunger extended) but the button has not been pressed this signifies that the plunger has not moved / the solenoid has failed.
I have represented the solenoid as an LED in the code for initial testing before I wire up the solenoid. The solenoid power will be supplied via a relay with the arduino digital output doing the relaying switching.
The code below.
Thanks guys.
// The LED represents a solenoid
//
int ledPin = 12; // the number of the LED pin
int buttonPin = 2;
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 1000; // milliseconds of LED (solenoid) on-time
long OffTime = 1000; // milliseconds of LED (solenoid) off-time
int cycles =0; // count the number of complete cycles
int reading = LOW; // button state
int ledState = LOW; // ledState used to set the LED
int lastButtonState = LOW; // previous button state
void setup()
{
// set the digital pins & serial
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
Serial.println("Inside void loop");
delay(1000);
while (ledState == digitalRead(buttonPin)) // enter while loop if the led state is the same as the button state
{
Serial.println("Inside Do While loop"); // display the led and button state for debugging
Serial.print("LED state (before):");
Serial.println(ledState);
Serial.print("Button state (before):");
Serial.println(digitalRead(buttonPin));
reading = digitalRead(buttonPin);
Serial.print("reading value (before):");
Serial.println(reading);
if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime) && (reading == HIGH)) // enter "IF" led is HIGH (on) & the time has exceeded the OnTime & button is HIGH
{
Serial.println("Inside IF ");
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
Serial.print("LED is (inside IF):");
Serial.println(ledState);
delay(1000);
cycles = cycles + 1;
while(digitalRead(buttonPin) == LOW)
{
Serial.println("Inside WHILE reading");
} // do nothing
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime) && (reading == LOW)) // enter "ELSE IF" led is LOW (off) & the time has exceeded the OffTime & button is LOW
{
Serial.println("Inside ELSE IF");
Serial.print("currentMills: ");
Serial.println(currentMillis);
Serial.print("previousMills: ");
Serial.println(previousMillis);
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
Serial.print("LED is (inside ELSE IF):");
Serial.println(ledState);
delay(1000);
while(digitalRead(buttonPin) == LOW)
{
Serial.println("Inside WHILE reading");
} // do nothing
}
Serial.print("LED is :");
Serial.println(ledState);
Serial.print("Number of cycles:");
Serial.println(cycles);
lastButtonState = digitalRead(buttonPin);
Serial.print("Last Button state :");
Serial.println(lastButtonState);
Serial.println("End of loop");
Serial.println("");
}
Serial.println("Outside Do While loop");
Serial.print("Number of cycles:");
Serial.println(cycles);
Serial.println("");
//while (1)
//{ }
} // end void loop