|
| 1 | +// SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// Motorized fader demo |
| 4 | +// capsense implementation by @todbot / Tod Kurt |
| 5 | + |
| 6 | +#include <Bounce2.h> |
| 7 | + |
| 8 | +const int pwmA = 12; // motor pins |
| 9 | +const int pwmB = 11; |
| 10 | + |
| 11 | +const int fader = A0; // fader pin |
| 12 | +int fader_pos = 0; |
| 13 | +float filter_amt = 0.75; |
| 14 | +float speed = 1.0; |
| 15 | + |
| 16 | +int saved_positions[] = { 230, 180, 120, 60 } ; |
| 17 | +int current_saved_position = 1 ; |
| 18 | + |
| 19 | +const int num_buttons = 4; |
| 20 | +const int button_pins[num_buttons] = {10, 9, 8, 7}; // feather silk != arduino pin number. 10, 9, 6, 5 on board |
| 21 | +Bounce buttons[num_buttons]; |
| 22 | +bool motor_release_state = false; // to handle motor release |
| 23 | + |
| 24 | +class FakeyTouch |
| 25 | +{ |
| 26 | + public: |
| 27 | + FakeyTouch( int apin, int athreshold = 300 ) { // tune the threshold value to your hardware |
| 28 | + pin = apin; |
| 29 | + thold = athreshold; |
| 30 | + } |
| 31 | + void begin() { |
| 32 | + baseline = readTouch(); |
| 33 | + } |
| 34 | + int readTouch() { |
| 35 | + pinMode(pin, OUTPUT); |
| 36 | + digitalWrite(pin,HIGH); |
| 37 | + pinMode(pin,INPUT); |
| 38 | + int i = 0; |
| 39 | + while( digitalRead(pin) ) { i++; } |
| 40 | + return i; |
| 41 | + } |
| 42 | + bool isTouched() { |
| 43 | + return (readTouch() > baseline + thold); |
| 44 | + } |
| 45 | + int baseline; |
| 46 | + int thold; |
| 47 | + int pin; |
| 48 | +}; |
| 49 | + |
| 50 | +const int touchpin_F = A3; |
| 51 | +FakeyTouch touchF = FakeyTouch( touchpin_F ); |
| 52 | + |
| 53 | + |
| 54 | +void setup() { |
| 55 | + Serial.begin(9600); |
| 56 | + delay(1000); |
| 57 | + pinMode (pwmA, OUTPUT); |
| 58 | + pinMode (pwmB, OUTPUT); |
| 59 | + analogWriteFreq(100); |
| 60 | + analogWrite(pwmA, 0); |
| 61 | + analogWrite(pwmB, 0); |
| 62 | + for (uint8_t i=0; i< num_buttons; i++){ |
| 63 | + buttons[i].attach( button_pins[i], INPUT_PULLUP); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +int last_fader_pos = fader_pos; |
| 68 | + |
| 69 | + |
| 70 | +void loop() { |
| 71 | + for (uint8_t i=0; i< num_buttons; i++){ |
| 72 | + buttons[i].update(); |
| 73 | + if( buttons[i].fell()) { |
| 74 | + current_saved_position = i; |
| 75 | + go_to_position(saved_positions[current_saved_position]); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if( touchF.isTouched()){ |
| 80 | + motor_release_state = true; |
| 81 | + analogWrite(pwmA, 0); |
| 82 | + analogWrite(pwmB, 0); |
| 83 | + delay(60); |
| 84 | + } |
| 85 | + else{ |
| 86 | + motor_release_state = false; |
| 87 | + go_to_position(saved_positions[current_saved_position]); |
| 88 | + } |
| 89 | + |
| 90 | + fader_pos = int( (filter_amt * last_fader_pos) + ( (1.0-filter_amt) * int(analogRead(fader) / 4 )) ); |
| 91 | + if (abs(fader_pos - last_fader_pos) > 1) { |
| 92 | + Serial.println(fader_pos); |
| 93 | + if (motor_release_state==false){ |
| 94 | + go_to_position(saved_positions[current_saved_position]); |
| 95 | + } |
| 96 | + last_fader_pos = fader_pos; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void go_to_position(int new_position) { |
| 101 | + fader_pos = int(analogRead(fader) / 4); |
| 102 | + while (abs(fader_pos - new_position) > 4) { |
| 103 | + if (fader_pos > new_position) { |
| 104 | + speed = 2.25 * abs(fader_pos - new_position) / 256 + 0.2; |
| 105 | + speed = constrain(speed, -1.0, 1.0); |
| 106 | + if (speed > 0.0) { |
| 107 | + analogWrite(pwmA, 255); |
| 108 | + analogWrite(pwmB, 0); |
| 109 | + } |
| 110 | + } |
| 111 | + if (fader_pos < new_position) { |
| 112 | + speed = 2.25 * abs(fader_pos - new_position) / 256 - 0.2; |
| 113 | + speed = constrain(speed, -1.0, 1.0); |
| 114 | + if (speed > 0.0) { |
| 115 | + analogWrite(pwmA, 0); |
| 116 | + analogWrite(pwmB, 255); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + fader_pos = int(analogRead(fader) / 4); |
| 121 | + |
| 122 | + } |
| 123 | + analogWrite(pwmA, 0); |
| 124 | + analogWrite(pwmB, 0); |
| 125 | +} |
0 commit comments