Detect button press while in a For loop..

Hey guys!

Just got this for Christmas and only am just now getting around to playing with it! Love it so far!

Before now I've only created things from the booklet I got and nothing fancy.

I am now trying to do something of my own and I've gotten so close but not quite there!

Setup:

1x Servo
1x Laser
1x Push Button

Currently it works just fine, if I push the button at the end of the For statement. But anytime during the For it's obviously in the loop and can't detect the press. I tried doing it two ways but couldn't seem to get it to work... any thoughts?

#include <Servo.h>

Servo myservo;
const int button1Pin = 2;
int laserPin = 10;
int pos = 0;
bool toggle;
bool RunStatus;

void laserOFF() {
  
      digitalWrite(laserPin, LOW);
      RunStatus = false;
      
}

void laserON() {

      digitalWrite(laserPin, HIGH);
      RunStatus = true;
	  
}

void moveServo() {

  for (pos = 0; pos <= 75; pos += 2) {
    // in steps of 1 degree
    myservo.write(pos);
    delay(30);
  }
  
  for (pos = 75; pos >= 0; pos -= 2) {
    myservo.write(pos);              
    delay(30);                   
  }
  
}


void setup ()
{
   myservo.attach(9);
   pinMode(button1Pin, INPUT);
   pinMode (laserPin, OUTPUT); // define the digital output interface 13 feet
   RunStatus = true;
   digitalWrite(laserPin, HIGH);

}
void loop () {

  byte button1State;
  button1State = digitalRead(button1Pin);

if (button1State == LOW) {

  toggle = !toggle;
  delay(400);

  if (toggle == false) {
  
      laserON();
      RunStatus = true;

    } else {
		
      laserOFF();
      RunStatus = false;
	  
    }
  }

  if (RunStatus == true) {
  
  moveServo();
  
  }
}

Attempt #2 - Adding in an If statement to the For loop.

#include <Servo.h>

Servo myservo;
const int button1Pin = 2;
int laserPin = 10;
int pos = 0;
bool toggle;
bool RunStatus;

void laserOFF() {
  
      digitalWrite(laserPin, LOW);
      RunStatus = false;
      
}

void laserON() {

      digitalWrite(laserPin, HIGH);
      RunStatus = true;
	  
}

void moveServo() {

  for (pos = 0; pos <= 75; pos += 2) {
	  
	  if (RunStatus == true) { ; What I added in for Attempt 2
	  
    myservo.write(pos);
    delay(30);
	
	}
  }
  
  for (pos = 75; pos >= 0; pos -= 2) {
	  
	  if (RunStatus == true) {
	  
    myservo.write(pos);              
    delay(30);
	
	}
  }
}


void setup ()
{
   myservo.attach(9);
   pinMode(button1Pin, INPUT);
   pinMode (laserPin, OUTPUT); // define the digital output interface 13 feet
   RunStatus = true;
   digitalWrite(laserPin, HIGH);

}
void loop () {

  byte button1State;
  button1State = digitalRead(button1Pin);

if (button1State == LOW) {

  toggle = !toggle;
  delay(400);

  if (toggle == false) {
  
      laserON();
      RunStatus = true;

    } else {
		
      laserOFF();
      RunStatus = false;
	  
    }
  }

  if (RunStatus == true) {
  
  moveServo();
  
  }
}

Welcome - good you are exploring, best way to learn

Time to go read, understand and play with
Blink Without Delay

Then there is: Gammon Forum : Electronics : Microprocessors : Blink sketch explained

.

Sounds like a candidate for an interrupt on the pushbutton, to toggle the run status, then check the status in your MoveServo function (which could be in the main loop)

Or wire the button to power directly the laser without going through the arduino :slight_smile:

Delta_G had the right idea IMO.

I was able to change how I moved the Servo and it works just fine now :slight_smile:

#include <Servo.h>

Servo myservo;

const int button1Pin = 2;


int laserPin = 10;
int pos = 0;
bool toggle;
bool RunStatus;
int randomnumber;
int randomnumber2;
int DirectionStatus = 1;

void laserOFF() {
  
      digitalWrite(laserPin, LOW);
      RunStatus = false;
      
}

void laserON() {
      digitalWrite(laserPin, HIGH);
      RunStatus = true;
}

void setup() {
   myservo.attach(9);
   pinMode(button1Pin, INPUT);
   pinMode (laserPin, OUTPUT); // define the digital output interface 13 feet
   RunStatus = true;
   digitalWrite(laserPin, HIGH);
    DirectionStatus = 2;
    myservo.write(20);
    Serial.begin(9600);
}

void loop() {

  byte button1State;

  button1State = digitalRead(button1Pin);

if (button1State == LOW) {
  toggle = !toggle;
  delay(400);

  if (toggle == false) {
      laserON();

    } else {
      laserOFF();
    }
  }

randomnumber = random(1, 4);
randomnumber2 = random(40, 70);

if (pos >= 75) {
  DirectionStatus = 2;
}

if (pos <= 0) {
  DirectionStatus = 1;
}

if (RunStatus == true) {
  if (DirectionStatus == 1) {
  if (pos <= 75) {
    pos += randomnumber;
    myservo.write(pos);
  }
}

  if (DirectionStatus == 2) {
    
  if (pos >= 1) {
    pos -= randomnumber;
    myservo.write(pos);
  }
}
}

delay(randomnumber2);
}