Capacitive Sensor to Control Stepper Motor


I am trying to use a capacitive sensor to control a stepper motor. When the tick values increase to a certain value I want the stepper motor to move forward until they go back to their original value. I am using the typical capacitive sensor code as follows and an adafruit motorshield v2 stepper motor code.

#include <CapacitiveSensor.h>

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

void setup()
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example

void loop()
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);

Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug windown spacing

Serial.print(total1); // print sensor output 1

delay(100); // arbitrary delay to limit data

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

myMotor->setSpeed(10); // 10 rpm

void loop() {
Serial.println(“Single coil steps”);
myMotor->step(100, FORWARD, SINGLE);


I’m pretty new to arduino and object oriented programming, but I basically want to use the cap sensor code to trigger the stepper motor code.