Stap 4: Code
De volgende code wordt gebruikt om te controleren de motor op basis van het ontwerp van de RC-kring. De CapacitiveSensor Library is gebruikt ter vereenvoudiging van het proces van teledetectie. U kunt vinden en deze library downloaden hier. De opmerkingen in de code zal hopelijk de code doel verlichten.
#include CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6. Pin 4 is the signal pin and pin 6 is the sensor pin.#define pin1 12//these are the Arduino pins that we use to activate coils 1-4 of the stepper motor #define pin2 13 #define pin3 10 #define pin4 11 #define delaytime x //delay time in ms to control the stepper motor delaytime.int x = 8;//8 is about the fastest that can yield reliable operation without missing steps void Step_A(){ digitalWrite(pin1, HIGH);//turn on coil 1 digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); } void Step_B(){ digitalWrite(pin1, LOW); digitalWrite(pin2, HIGH);//turn on coil 2 digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); } void Step_C(){ digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, HIGH); //turn on coil 3 digitalWrite(pin4, LOW); } void Step_D(){ digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, HIGH); //turn on coil 4 } void step_OFF(){ digitalWrite(pin1, LOW); //power all coils down digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); }//these functions run the above configurations in forward and reverse order //the direction of a stepper motor depends on the order in which the coils are turned on. void forward(){//one tooth forward step_OFF(); long total2 = cs_4_6.capacitiveSensor(30); Serial.print(total2); // serial print sensor output 2. This is necessary to ensure the signal is being updated initially. Serial.print("\n"); delay(delaytime); Step_A(); delay(delaytime); Step_B(); delay(delaytime); Step_C(); delay(delaytime); Step_D(); delay(delaytime);}void backward(){//one tooth backward step_OFF(); long total2 = cs_4_6.capacitiveSensor(30); Serial.print(total2); // serial print sensor output 2. This is necessary to ensure the signal is being updated initially. Serial.print("\n"); delay(delaytime); Step_D(); delay(delaytime); Step_C(); delay(delaytime); Step_B(); delay(delaytime); Step_A(); delay(delaytime); }/***************************setup function****************************************************/void setup() { Serial.begin(9600);//start serial communication pinMode(pin1, OUTPUT); // initiate motor pins pinMode(pin2, OUTPUT); pinMode(pin3, OUTPUT); pinMode(pin4, OUTPUT);}/***************************main loop*********************************************************/void loop() { delaytime = x; int numberOfSteps = 30; long total2 = cs_4_6.capacitiveSensor(30); Serial.print(total2); // print sensor output 2 Serial.print("\n"); if (total2 > 2000){ //Capacitive Sensing Conditional. NOTE: THESE THRESHOLD VALUES VARY WITH FOIL SIZE, RESISTOR SIZE AND OTHER VARIABLES. Be sure to test out these values prior to project construction. while(numberOfSteps>0) //After increasing by a certain threshold between two measuring periods, initiate stepper motor backward { backward(); //going backward numberOfSteps --;}} if (total2 < 500 && total2 > 150){ //If decreasing past a threshold, initiate stepper motor forward while(numberOfSteps>0) { forward(); //going forward numberOfSteps --;}} if (total2 < 150){ // This condition is made to ensure noise is kept our of the equation. step_OFF(); } delay(50); }