Hi all. This is my first post. I've always been a great PC builder and power user, but I never really got much into coding past Microsoft Basic. That actually works great for me since I'm a CNC programmer by trade and a lot of my coding is manual and some of the advance g-coding I have to use consists of While/Do statements and loops. I've been playing around with the Arduino a bit and have been thinking of trying to automate a couple of our simple processes that we do manually that, in short, suck for the operator. I've ran into a bit of a standstill due to my lack of knowledge and was hoping I could get a bit of help.
Although I have a stepper motor and driver ordered and on the way, I've been testing out my code and process using LED blinks. My code is still programmed to blink the LED, but it will eventually be replaced with a command to rotate the stepper motor one full revolution.
My first problem in this code is that I need to blink the LED 20 times. However, I need to be able to pause during the middle and then resume. Let's say the operator pushes the button and it blinks 10 times--they need to be able to press the button, pause the blinking, and then press the button again to resume--with the next blink still being the 11th. If I need to, I can hookup a 2nd button instead of the first to be the pause button, so one button can be Start, and the other can be Pause/Resume. (Side question--how can I turn the number 10 for number of blinks into a variable set at the top?)
Here's my current code that works (with my test button) to flash the LED:
void setup(){
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop(){
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
//LED on when not pressed
if (sensorVal == LOW) {
digitalWrite(13, LOW);
}
else {
for(int i = 0; i < 10; i++)
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(500);
}
}
}
My 2nd problem is, I have another process almost identical except the rotation/blink will occur one time---BUT I'm using an optical sensor and based on the nature of the process at the end of the loop I need to make sure that the sensor is not still blocked. The process is that an arm is moved into a position that the sensor is NOT blocked. It then moves back to where the sensor is blocked. Then the rotation/blink should occur one time. In the above code, after the blinking is done, it checks the button again. If it is still pressed down it immediately starts blinking another sequence. ON this setup, after the moving in/out is done a number of times the operator pulls the arm back (sensor still blocked) and has to manually change a part out. I do not want the rotation to happen again until the sensor is unblocked, and then blocked again. I do not know how to make the end of the program wait until the sensor is unblocked again before it loops to the top and waits for the sensor to be blocked.
In short:
- How can I pause during the middle of my blinking sequence, and then resume? (Also how can I change the number of blinks into a variable?)
- How can I make the end of a loop wait for the button state to change again?
Thanks in advance for all the help. I know this should be a simple problem. If it were CNC C-Code I would be able to solve it pretty simply. I may just be overthinking the problem in this coding.