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();
}
}