Servo motors - triggered by sequence count #2 and 4 (green and blue).
I tried to make it so when the button was pressed on count 2 or 4, the servo motors would continue to move as long as the count remained at 2 or 4, but I couldn't get that to work. There is some conditional I'm missing, or perhaps have placed in the wrong spot in the programming void.loop. Not quite sure. I'll figure it out though. For now though, what is here will suffice for this lab exercise.
here's the code:
////////3///////servo library, name servo motors//////
#include <Servo.h>
Servo Green;
Servo Blue;
/////////3///////////
/////////////////////////
const int led01 = 8;
const int led02 = 9;
const int led03 = 4;
const int led04 = 5;
const int led05 = 6;
int count = 0;
int dir = 1;
////////////////////
const int buttonPin = 7;
int buttonPress = 0;
//const int led06 = 11;
int bright = 0;
int dirB = 1;
//////////////////
///2///////////////
const int potPin = A0;
int potValue = 0;
int newValue = 0;
int dlyVar = 0;
///2//////////////
void setup() {
Serial.begin(9600);
pinMode(led01, OUTPUT);
pinMode(led02, OUTPUT);
pinMode(led03, OUTPUT);
pinMode(led04, OUTPUT);
pinMode(led05, OUTPUT);
///////////////////////////
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
///////////////////////////
/////////////3/////////
Green.attach(3);
Blue.attach(11);
///////////3/////////
}
void loop() {
/////2//////////////
potValue = analogRead(potPin);
newValue = map(potValue, 0, 1023, 50, 1000);
dlyVar = newValue;
Serial.print("potValue = ");
Serial.print(potValue);
Serial.print(" ");
Serial.print("newValue = ");
Serial.print(newValue);
Serial.print(" ");
/////2//////////////
//////////////////////////////
buttonPress = digitalRead(buttonPin);
Serial.print("buttonPress = ");
Serial.print(buttonPress);
Serial.print(" ");
if(buttonPress==1){
// digitalWrite(led06, LOW);
/////////////////////////////////////
if(count >= 6){
dir=-1;
}
else if(count <=0){
dir=1;
}
count = count+1*dir;
delay(dlyVar);
///^^2^^////changed delay amount to be controlled by pot////////
if(count==0){
digitalWrite(led01, LOW);
digitalWrite(led02, LOW);
digitalWrite(led03, LOW);
digitalWrite(led04, LOW);
digitalWrite(led05, LOW);
}
else if(count==1){
digitalWrite(led01, HIGH);
digitalWrite(led02, LOW);
digitalWrite(led03, LOW);
digitalWrite(led04, LOW);
digitalWrite(led05, LOW);
}
else if(count==2){
digitalWrite(led01, LOW);
digitalWrite(led02, HIGH);
digitalWrite(led03, LOW);
digitalWrite(led04, LOW);
digitalWrite(led05, LOW);
///////////3/////////
Green.write(90);
delay(500);
Green.write(0);
delay(10);
////////////3////////
}
else if(count==3){
digitalWrite(led01, LOW);
digitalWrite(led02, LOW);
digitalWrite(led03, HIGH);
digitalWrite(led04, LOW);
digitalWrite(led05, LOW);
}
else if(count==4){
digitalWrite(led01, LOW);
digitalWrite(led02, LOW);
digitalWrite(led03, LOW);
digitalWrite(led04, HIGH);
digitalWrite(led05, LOW);
///////3//////////
Blue.write(90);
delay(500);
Blue.write(0);
delay(10);
/////3///////////
}
else if(count==5){
digitalWrite(led01, LOW);
digitalWrite(led02, LOW);
digitalWrite(led03, LOW);
digitalWrite(led04, LOW);
digitalWrite(led05, HIGH);
}
else if(count==6){
digitalWrite(led01, LOW);
digitalWrite(led02, LOW);
digitalWrite(led03, LOW);
digitalWrite(led04, LOW);
digitalWrite(led05, LOW);
}
Serial.print("sequence-count = ");
Serial.print(count);
Serial.println(" ");
}
//////////////////////////
if(buttonPress==0){
bright = bright + dirB;
if(bright>+255){
dirB = -1;
}
else if(bright<=0){
dirB = 1;
////////////////3///////////////
if(count==2){
Green.write(90);
delay(500);
Green.write(0);
delay(10);
}
if(count==4){
Blue.write(90);
delay(500);
Blue.write(0);
delay(10);
}
///////////3////////////////
}
//analogWrite(led06, bright);
Serial.print("sequence hold = ");
Serial.print(count);
Serial.println(" ");
/////////////////////////////
}
}
No comments:
Post a Comment