I've written some code and wanted it to execute with a momentary button push and then stop or deenergize my relays and motors with another. Any help with this would be greatly appreciated. Oh the problem is it initiates the code but will not stop it. Here is the code:
int buttonPin;
int x;
int run;
void setup()
{
pinMode(46,OUTPUT); // set Pin46 as PUL
pinMode(48,OUTPUT); // set Pin48 as DIR
pinMode(22, OUTPUT); // Configure pin 22 as an Output
pinMode(24, OUTPUT); // Configure pin 24 as an Output
pinMode(26, OUTPUT); // configure pin 26 as an Output
pinMode(28, OUTPUT); // configure pin 28 as an Output
run=0;
buttonPin = 7;
pinMode(7, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(7) == LOW)
{
if(run == 0)
{
run = 255;
}
else
{
run = 0;
}
}
if(run > 0)
{
digitalWrite(48,HIGH); // set high level direction
for(x = 0; x < 25000; x++) // repeat 25000 times a revolution when setting 25000 on driver
{
digitalWrite(46,HIGH); // Output high
delayMicroseconds(10); // set rotate speed
digitalWrite(46,LOW); // Output low
delayMicroseconds(10); // set rotate speed
// Energize Valve
digitalWrite(22, LOW);
digitalWrite(24, HIGH);
digitalWrite(26, HIGH);
digitalWrite(28, LOW);
}
}}