Hey sorry all!
Here is the entire code, thought it might be a bum to see all. @Steve, I know. I uploaded the working code. This was the latest thing I tried, but it is a mess..... So I was hesitant to upload that ![]()
#include <CapacitiveSensor.h> //Library for fader touch sensitivity
#include <Keyboard.h>
//Array to store the random position values
int progress;
long randomVals[3];
//Arduino Pin Assignments
int motorDown = 9; //H-Bridge control to make the motor go down
int motorUp = 8; //H-Bridge control to make the motor go up
int enablePin = 10; //H-Bridge enable to PWM 11 to control speed
//Inputs
int wiper = A0; //Position of fader relative to GND (Analog 1)
//Variables
double faderMax = 0; //Value read by fader's maximum position (0-1023)
double faderMin = 0; //Value read by fader's minimum position (0-1023)
int faderChannel = 1; //Value from 1-8
int value; //To get the random position for the motor
int attempt = 0; //To get the 3 user inputs
int counter = 0; //To differentiate the different touches
//For the pause between the random locations
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long previousPattern = 0;
unsigned long currentPattern = 0;
unsigned long previousTimer = 0;
unsigned long currentTimer = 0;
unsigned long intervalLocation = 3000;
unsigned long intervalPattern = 3000;
//Touchsensor
CapacitiveSensor cs_7_8 = CapacitiveSensor(4, 3);
unsigned long csSum;
volatile bool touched = false; //Is the fader currently being touched?
volatile bool perform = false;
int errorMargin = 50;
//-----------------------------------------------------------------------------------------------------------------------------
void setup() {
pinMode (motorUp, OUTPUT);
pinMode (motorDown, OUTPUT);
pinMode (enablePin, OUTPUT);
randomSeed(analogRead(5));
calibrateFader();
}
//-----------------------------------------------------------------------------------------------------------------------------
void loop() {
checkTouch();
while (perform) {
checkTouch();
showPattern();
}
}
//-----------------------------------------------------------------------------------------------------------------------------
void createValues() {
currentMillis = millis();
for (int i = 0; i < 3; i++) {
if (currentMillis - previousMillis >= intervalLocation && progress == i) {
randomVals[i] = random(1020);
Serial.println(randomVals[i]);
updateFader(randomVals[i]);
previousMillis = millis();
progress++;
}
}
perform = true;
if (progress == 3) {
progress = 0;
}
}
//-----------------------------------------------------------------------------------------------------------------------------
void showPattern() {
currentPattern = millis();
for (int i = 0; i < 3; i++) {
if (currentPattern - previousPattern >= intervalPattern) {
updateFader(randomVals[i]);
previousPattern = millis();
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------
//Calibrates the min and max position of the fader
void calibrateFader() {
//Send fader to the top and read max position
digitalWrite(motorUp, HIGH);
digitalWrite(enablePin, 95);
delay(500);
digitalWrite(motorUp, LOW);
digitalWrite(enablePin, HIGH);
faderMax = analogRead(wiper);
//Send fader to the bottom and read max position
digitalWrite(motorDown, HIGH);
digitalWrite(enablePin, HIGH);
delay(500);
digitalWrite(motorDown, LOW);
digitalWrite(enablePin, HIGH);
faderMin = analogRead(wiper);
}
//-----------------------------------------------------------------------------------------------------------------------------
void updateFader(int position) {
currentTimer = millis();
int currentTimerF = millis();
int previousTimerF = 0;
if (position < analogRead(wiper) - 10 && position > faderMin && !touched && currentTimer - previousTimer <= 1000) {
digitalWrite(motorDown, HIGH);
digitalWrite(enablePin, HIGH);
while (position < analogRead(wiper) - 10 && !touched) {}; //Loops until motor is done moving
digitalWrite(motorDown, LOW);
previousTimer = millis();
}
if (position > analogRead(wiper) + 10 && position < faderMax && !touched && currentTimerF - previousTimerF <= 1000) {
digitalWrite(motorUp, HIGH);
digitalWrite(enablePin, HIGH);
while (position > analogRead(wiper) + 10 && !touched) {};
digitalWrite(motorUp, LOW);
previousTimerF = millis();
}
}
//-----------------------------------------------------------------------------------------------------------------------------
//Check to see if the fader is being touched
void checkTouch() {
long cs = cs_7_8.capacitiveSensor(80); //Sensor resolution is set to 80, for higher sensitivity increase this value (to 2000 approx)
csSum += cs;
if (cs > 100 && csSum >= 3800) { //Arbitrary number
int location = analogRead(A0);
touched = true;
switch (counter) {
case 0: //Show pattern
createValues();
counter++;
break;
case 1: //Read user input
for (int i = 0; i < 3; i++) {
if (touched && location < randomVals[i] + errorMargin && location > randomVals[i] - errorMargin && attempt == i) {
attempt++;
}
}
break;
}
} else {
touched = false;
}
//Reset if attempted
if (attempt == 3) {
Keyboard.begin();
Keyboard.print("YEAH");
attempt = 0;
perform = false;
delay(3000);
counter = 0;
Keyboard.end();
}
}
Great tip of the while! Did not know that.