Two, Two Pin Toggle Switches Not working

I’ve a Arduino set up that moves a stepper motor an X amount, after each series of steps it causes an LED to flash (ultimately this will trigger a camera), drives a small motor and has 4 potentiometer control for an RGB LED.

It all works.

I next decided I needed two on/off switches. For this I chose two, two pin toggle switches. One switch to turn off the motors and the LED which flashes, the second switch to turn off the RGB LED.

I though this would be simple but I’ve hit a wall this last couple of weeks and cant get it to work.

I wonder if anyone could take a look at the code and offer me any inspiration as to why the scripts are not working?

I’ve also attached the drawing of how I have it wired.

Many thanks in advance.

/* 
Controls Stepper Motor & DC Motor with an on/off switch with debounce. 
The Stepper should move, pause and blink the LED before moving again.
DC Motor runs constantly.

Also includes RGB LED with brightness control.
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

const int buttonPin1 = 2; // Switch 01
const int buttonPin2 = 3; // Switch 02
const int ledPin = 13;

int cycle = 0;

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;

int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BrightPotPin = 3;

int Rval;
int Gval;
int Bval;
int Brightval;

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 2);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);


void setup() {

 Serial.begin(9600);           // set up Serial library at 9600 bps

 pinMode (buttonPin1, INPUT_PULLUP); // button pin is input ‘pull-up’ - HIGH = off, LOW = on
        pinMode (buttonPin2, INPUT_PULLUP); // button pin is input ‘pull-up’ - HIGH = off, LOW = on
 pinMode (ledPin, OUTPUT);   // led pin is output (obviously)

        pinMode(RledPin, OUTPUT);
        pinMode(GledPin, OUTPUT);
        pinMode(BledPin, OUTPUT);    
        pinMode(RpotPin, INPUT);    
        pinMode(GpotPin, INPUT);
        pinMode(BpotPin, INPUT);    
        pinMode(BrightPotPin, INPUT);

 AFMS.begin(1600);   // create with the frequency 1.6KHz (this is the default)
  
 myMotor->setSpeed(50); // set continuous motor speed (0 - 255)
 myStepper->setSpeed(30);   // set stepper motor speed  
  
}

void loop() {
  
        // if the SPST switch 2 is engaged…
        if (digitalRead(buttonPin2) == LOW) {
  
                Brightval = analogRead(BrightPotPin);
                Brightval = map(Brightval, 0, 1023, 0, 255);
  
                Rval = analogRead(RpotPin);
                Rval = map(Rval, 0, 1023, 0, 255);
                Rval = Rval * (Brightval/255.0);
                Gval = analogRead(GpotPin);
                Gval = map(Gval, 0, 1023, 0, 255);
                Gval = Gval * (Brightval/255.0);
                Bval = analogRead(BpotPin);
                Bval = map(Bval, 0, 1023, 0, 255);
                Bval = Bval * (Brightval/255.0);

                analogWrite(RledPin, Rval);
                analogWrite(GledPin, Gval);
                analogWrite(BledPin, Bval);

        }
     

 // if the SPST switch 1 is engaged…
   if (digitalRead(buttonPin1) == LOW) {

 // step motor for first 50 cycles
 if (cycle <= 49) {
 myMotor->run(FORWARD); // start the continuous motor (it may be already on from previous cycle)
 myStepper->step(1, FORWARD, SINGLE); // step stepper motor forward
 }


 // at cycle 50, pause stepper (actually just don’t advance it), turn led on (take photo)
 if (cycle == 50) {
 digitalWrite(ledPin, HIGH); // turn led on
 }

 // NB nothing will happen now from cycle 51 to 499…

 // at cycle 500, turn led off & reset
 // NB cycle 500 is a guess, I am not sure how long 450 cycles ‘delay’ will take in time (probably pretty fast)
 // we probably need to eventually do something clever to time this and unpause after a time has elapsed rather than no. cycles
 // BUT it is good to do this without using ‘delay’ command, so this loop continues and the pots affect the rgb led in real time
 if (cycle == 500) {
 digitalWrite(ledPin, LOW); // turn led off

 cycle = -1; // start again…
 }

 cycle ++; // increment cycle counter
     
   }
  
}

Your code would be easier to debug if you put some simple Serial.println in there.

const int buttonPin1 = 2; // Switch 01
const int buttonPin2 = 3; // Switch 02
const int ledPin = 13;

int cycle = 0;

int RledPin = 11;
int GledPin = 10;
int BledPin = 9;

int RpotPin = 0;
int GpotPin = 1;
int BpotPin = 2;
int BrightPotPin = 3;

First, why do you use pins 0 & 1 that are for Serial to USB?

Second, why are your button pins also your pot pins?

Also your IDE has a Tool called AutoFormat that could be your friend.
Also why do you use ints for pin numbers?

First, why do you use pins 0 & 1 that are for Serial to USB?

Psst, analogue pins.

AWOL:
Psst, analogue pins.

Whups, that's twice in a week or less too. I'm use to writing A0, A1, A2 just to keep it straight.

The rest of the code looks like it should work though I would replace the map() functions with / 4.

Does it work when both switches are on?

How do you know it is not working?

That cycle++ will count awfully quickly.

...R

Thanks for the suggestions!

I've had a bit of a break through!

I renamed the analogue Pins for the RGB & Brightness A1, A2, A3 & A4.

The motor switch now works in a fashion. Toggling it OFF seems to halt the stepper motor and the flashing LED. Though it doesn't necessarily finish its cycle, sometimes the flashing LED is left on.

The second motor runs continuously with the switch ON or OFF.

The RGB LED when switched ON works fine when OFF it locks out the RGB controls rather than turn the LED off.

When the RGB LED is switch is turned OFF and the motor switch is ON, the stepper runs continuously.

I've swapped the pin numbering round to see if its something physical but the same things happens when pin2 is on pin3 and visa versa.

If that makes sense?

I'm assuming the if statements need tweaking? I can't believe the on/off switches are so complicated! I thought it would be plain sailing once I'd figured out the RGB LED, the switches were an after thought.

I'll try and add in Serial.println to see whats now going on.

Thanks again,
Angus

I don't seem to recall your code doing anything to 'clean up' whatever state the leds or motors had when the switch was last ON when it's been turned OFF.

Code only does what you tell it, not what you think it should.