Line 24: |
Line 24: |
| | | |
| [https://circuitjournal.com/arduino-serial-to-spreadsheet ArduSpreadsheet add-on] | | [https://circuitjournal.com/arduino-serial-to-spreadsheet ArduSpreadsheet add-on] |
| + | |
| + | ===Code=== |
| + | |
| + | //include relevant libraries |
| + | |
| + | #include <Wire.h> |
| + | #include "Adafruit_MPR121.h" |
| + | |
| + | #include <AccelStepper.h> |
| + | |
| + | //define MPR121 info |
| + | #ifndef _BV |
| + | #define _BV(bit) (1 << (bit)) |
| + | #endif |
| + | |
| + | Adafruit_MPR121 cap = Adafruit_MPR121(); |
| + | |
| + | uint16_t lasttouched = 0; |
| + | uint16_t currtouched = 0; |
| + | |
| + | //define DRV8825 stuff: |
| + | #define dirPin 2 |
| + | #define stepPin 3 |
| + | #define motorInterfaceType 1 |
| + | |
| + | // Create a new instance of the AccelStepper class: |
| + | AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); |
| + | |
| + | //setup code here, to run once: |
| + | void setup() { |
| + | stepper.setMaxSpeed(1000); //set max stepper speed in steps per second |
| + | Serial.begin(9600); //start serial monitor |
| + | while (!Serial) { // needed to keep from starting too fast! |
| + | delay(10); |
| + | } |
| + | Serial.println("Electronic Esophagus test start"); |
| + | // Default address is 0x5A, if tied to 3.3V its 0x5B |
| + | // If tied to SDA its 0x5C and if SCL then 0x5D |
| + | if (!cap.begin(0x5A)) { |
| + | Serial.println("MPR121 not found, check wiring?"); |
| + | while (1); |
| + | } |
| + | Serial.println("MPR121 found!"); |
| + | |
| + | // to change threshold sensitivites (touched, released). Defaults are (12,6). Valuess from 0-255 = less-more sensitive. |
| + | cap.setThresholds(24, 12); |
| + | } |
| + | |
| + | // main code here, to run repeatedly: |
| + | void loop() { |
| + | // Get the currently touched pads |
| + | currtouched = cap.touched(); |
| + | |
| + | for (uint8_t i=0; i<12; i++) { |
| + | // it if *is* touched and *wasnt* touched before, alert! |
| + | if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) { |
| + | Serial.print(i); Serial.print('\t'); Serial.println(" touched"); |
| + | // set the position to 0: |
| + | stepper.setCurrentPosition(0); |
| + | // Run the motor forward at 400 steps/second until the motor reaches 24 steps (~0.125 revolutions): |
| + | while(stepper.currentPosition() != 12) |
| + | { |
| + | stepper.setSpeed(400); |
| + | stepper.runSpeed(); |
| + | } |
| + | delayMicroseconds(500); |
| + | } |
| + | |
| + | // if it *was* touched and now *isnt*, alert! |
| + | if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) { |
| + | Serial.print(i); Serial.print('\t'); Serial.println(" released"); |
| + | stepper.setCurrentPosition(0); |
| + | stepper.stop(); // Stop as fast as possible: sets new target |
| + | stepper.runToPosition(); // Now stopped after quickstop |
| + | delayMicroseconds(500); |
| + | } |
| + | } |
| + | |
| + | // reset our state |
| + | lasttouched = currtouched; |
| + | |
| + | // comment out "return" line for detailed data from the sensor! |
| + | return; |
| + | |
| + | // debugging info, what |
| + | Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX); |
| + | Serial.print("Filt: "); |
| + | for (uint8_t i=0; i<12; i++) { |
| + | Serial.print(cap.filteredData(i)); Serial.print("\t"); |
| + | } |
| + | Serial.println(); |
| + | Serial.print("Base: "); |
| + | for (uint8_t i=0; i<12; i++) { |
| + | Serial.print(cap.baselineData(i)); Serial.print("\t"); |
| + | } |
| + | Serial.println(); |
| + | |
| + | // put a delay so it isn't overwhelming |
| + | delay(100); |
| + | } |
| | | |
| ==People== | | ==People== |