Hello again,
Thank you for your replies they were very helpful.
I am having two problems at this point;
I did end up getting the motor to run continuously at the push of a button, and then stop at the second push of the button. However, it is extremely slow. Is there a way around this?
Secondly, I have three external buttons that are acting like they are being continuously pressed. The thing is that they were working before lunch; press button, turn light on, then press button again, turn light off. After lunch I come back, turn it back on, and it's now looping through the voids of the three external buttons. No, no one messed with my stuff.
The external buttons are Normally Open, connected to COM and MCP inputs 5, 6, 7.
Again, abbreviated code because of length:
int Button_B1 = 0;
int OLDButton_B1 = 0;
int Button_B2 = 0;
int OLDButton_B2 = 0;
int Button_B3 = 0;
int OLDButton_B3 = 0;
int Button_B4 = 0;
int OLDButton_B4 = 0;
int led1 = 0; //21 writes to Top led
int led2 = 1; //22 writes to 2nd led
int led3 = 2; //23 writes to 3rd led
int led4 = 3; //24 writes to 4th led
int led5 = 4; //25 writes to Bottom led
int BlueButton = 5; //reads from Blue Button
int GreenButton = 6; //reads from Green Button
int AmberButton = 7; //reads from Amber Button
Adafruit_MotorShield AFMStop(0x61); // Rightmost jumper is closed... 60 is Default address, no jumpers
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 2);
int BB = 0;
int GB = 0;
int AB = 0;
int BB2 = 0;
int OLDBB2 = 0;
int GB2 = 0;
int OLDGB2 = 0;
int AB2 = 0;
int OLDAB2 = 0;
int j = 0;
int stepCount = 0;
unsigned long PreviousOnBoardLEDMillis = 0;
unsigned long currentMillis = 0;
unsigned long PreviousLED1Millis = 0;
const int LEDDuration = 300; //number of millisecs that the LEDs are on
const int onBoardLEDInterval = 500;
const int LED1Interval = 500;
const int LED2Interval = 500;
const int LED3Interval = 500;
const int LED4Interval = 500;
const int LED5Interval = 500;
byte led1state = LOW;
void setup(void)
{
while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(115200);
Serial.println(F("Goofy Begin"));
Serial.println(F("-----------------------------------------"));
mcp.begin(); // use default address 0
AFMStop.begin(); // Start the top shield
myStepper1->setSpeed(400.0);
mcp.pinMode(BlueButton, INPUT);
mcp.pinMode(GreenButton, INPUT);
mcp.pinMode(AmberButton, INPUT);
mcp.pinMode(led1,OUTPUT);
mcp.pinMode(led2,OUTPUT);
mcp.pinMode(led3,OUTPUT);
mcp.pinMode(led4,OUTPUT);
mcp.pinMode(led5,OUTPUT);
LEDsAllOn ();
delay (1000);
LEDsAllOff();
}
void loop()
{
BB = mcp.digitalRead(BlueButton);
GB = mcp.digitalRead(GreenButton);
AB = mcp.digitalRead(AmberButton);
long unsigned clock = millis();
currentMillis = clock;
//UpdateLED1();
//UpdateLED2();
//UpdateLED3();
//UpdateLED4();
//UpdateLED5();
appButtons();
green_Button();
amber_Button();
blue_Button();
contStepForward();
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;
} // end loop
void blue_Button(){
if (BB == 0){
Serial.println(F("Blue Button Pressed"));
{BB2 = 1 - BB2;}
if (BB2 == 1 && OLDBB2 == 0){
delay (25);
j = 2;
delay(2);
}
if (BB2 == 0 && OLDBB2 == 1){
Serial.println(F("Blue Button Released"));
j = 0;
delay(25);
}
OLDBB2 = BB2;
}
} //end blue_Button
void green_Button(){
if (GB == 0){
Serial.println(F("Green Button Pressed"));
{GB2 = 1 - GB2;}
if (GB2 == 1 && OLDGB2 == 0)
{
delay (25);
LEDsFlashDown();
delay(2);
}
if (GB2 == 0 && OLDGB2 == 1){
Serial.println(F("Green Button Released"));
LEDsAllOff();
delay(25);
}
OLDGB2 = GB2;
}
} // end green_Button
void amber_Button(){
if (AB == 0){
Serial.println(F("Amber Button Pressed"));
{AB2 = 1 - AB2;}
if (AB2 == 1 && OLDAB2 == 0){
delay (25);
LEDsAllOn();
delay(2);
}
if (AB2 == 0 && OLDAB2 == 1){
Serial.println(F("Amber Button Released"));
LEDsAllOff();
delay(25);
}
OLDAB2 = AB2;
}
}
void appButtons(){
// Buttons
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
if (pressed){
switch (buttnum){
case 1:{
{Button_B1 = 1 - Button_B1;}
if(Button_B1 == 1 && OLDButton_B1 == 0){
LEDsAllOn ();
Serial.println(F("Button 1 Pressed"));
}
if (Button_B1 == 0 && OLDButton_B1 == 1){
Serial.println(F("Button 1 Released"));
LEDsAllOff ();
}
OLDButton_B1 = Button_B1;
break;
}
case 2: {
{Button_B2 = 1 - Button_B2;}
if(Button_B2 == 1 && OLDButton_B2 == 0){
Serial.println(F("Button 2 Pressed"));
LEDsFlashDown();
myStepper1->step(200, BACKWARD, DOUBLE); //Move Foward 1 mm
}
if (Button_B2 == 0 && OLDButton_B2 == 1){
Serial.println(F("Button 2 Released"));
LEDsAllOff();
}
OLDButton_B2 = Button_B2;
break;
}
case 3: {
{Button_B3 = 1 - Button_B3;}
if(Button_B3 == 1 && OLDButton_B3 ==0){
Serial.println(F("Button 3 Pressed"));
LEDsFlashUp();
}
if(Button_B3 ==0 && OLDButton_B3 ==1){
Serial.println(F("Button 3 Released"));
LEDsAllOff();
}
OLDButton_B3 = Button_B3;
break;
}
case 4: {
{Button_B4 = 1 - Button_B4;}
if(Button_B4 == 1 && OLDButton_B4 == 0){
Serial.println(F("Button 4 Pressed"));
//LEDsFlashDown();
//contStepForward();
j = 2;
}
if(Button_B4 == 0 && OLDButton_B4 == 1){
Serial.println(F("Button 4 Released"));
LEDsAllOff();
j = 0;
}
OLDButton_B4 = Button_B4;
break;
} // end 4
} // end buttnum
} // end pressed
} // end packet buffer
} // end appButtons
void LEDsAllOn (){
Serial.println (F("LEDsAllOn void"));
mcp.digitalWrite (led1, HIGH);
mcp.digitalWrite (led2, HIGH);
mcp.digitalWrite (led3, HIGH);
mcp.digitalWrite (led4, HIGH);
mcp.digitalWrite (led5, HIGH);
}
void LEDsAllOff (){
Serial.println (F("LEDsAllOff void"));
mcp.digitalWrite (led1, LOW);
mcp.digitalWrite (led2, LOW);
mcp.digitalWrite (led3, LOW);
mcp.digitalWrite (led4, LOW);
mcp.digitalWrite (led5, LOW);
}
void LEDsFlashDown(){
if(led5state == LOW){
if (CMFlashDown - PSMFlashDown >= MBSFlashDown){
PSMFlashDown += MBSFlashDown;
mcp.digitalWrite (led5, LOW);
delay(5);
mcp.digitalWrite (led1, HIGH);
delay(75);
mcp.digitalWrite (led2, HIGH);
delay(5);
mcp.digitalWrite (led1,LOW);
delay(75);
mcp.digitalWrite (led3, HIGH);
delay(5);
mcp.digitalWrite (led2, LOW);
delay(75);
mcp.digitalWrite (led4, HIGH);
delay(5);
mcp.digitalWrite (led3, LOW);
delay(75);
mcp.digitalWrite (led5, HIGH);
delay(5);
mcp.digitalWrite (led4, LOW);
delay(75);
mcp.digitalWrite (led5, LOW);
delay(1);
PSMFlashDown += MBSFlashDown;
}
else {
if (currentMillis - PSMFlashDown >= LEDDuration){
PSMFlashDown += LEDDuration;
}
}
}
} //end flashdown
void UpdateLED1(){
if(led1state == LOW){
if (currentMillis - PreviousLED1Millis >= LED1Interval) {
led1state = HIGH;
PreviousLED1Millis += LED1Interval;
}
}
else {
if (currentMillis - PreviousLED1Millis >= LEDDuration){
led1state = LOW;
PreviousLED1Millis += LEDDuration;
}
}
}
//did this for all 5 LEDs, removed for length errors
/*
void LEDsFlashDown(){
mcp.digitalWrite (led1, led1state);
mcp.digitalWrite (led2, led2state);
mcp.digitalWrite (led3, led3state);
mcp.digitalWrite (led4, led4state);
mcp.digitalWrite (led5, led5state);
}
*/
void contStepBackward(){
myStepper1->step(1, BACKWARD, DOUBLE);
}
void contStepForward(){
if (j == 2){
myStepper1->step(1, FORWARD, DOUBLE);
}
}