Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggling/scrolling through a string or character keymap using YOUR OWN buttons/sensors #6

Open
b-erickson opened this issue May 17, 2017 · 2 comments

Comments

@b-erickson
Copy link

b-erickson commented May 17, 2017

So, I've got an array of 2-3 capacitive touch sensors which can determine whether a user is swiping left or right (or similarly, up and down).

Thus far, I have my own code which reads the capacitive sensors, determines the direction of the swipe and either adds or subtracts (a value) depending on the swipe direction. This value is a whole number, from 0 to 30 (it could be from 0-10, or 0-100 - the exact range is not a concern) and it is printed on the serial monitor when it is changed. This system is effectively a number counter.

For each value or counter number in this 0-30 range, there is a corresponding string or message - such as "see you soon", or "how are you?" that is located within the const String keyMap.
Is this starting to make sense?

My question is; how should the program acknowledge a particular current counter value and similarly 'tie' this to one of the keyMap strings?

In essence, I'd like to be able to print the string on the serial monitor.
Later on, I'd like to be able to display these strings on an OLED screen.

I will update this and post the functioning code once I have this solved. Stay tuned.

@ChrisTMChen
Copy link

Looking at a similar problem, trying to navigate through strings on an old screen with a rollerball/joystick which is basically either an up or down movement.

@b-erickson
Copy link
Author

b-erickson commented May 18, 2017

Update:

Yesterday Scott reviewed and added code to my sketch, to address the functionality requirement I mentioned in the above post.

Preface:
At the time of the post, I had defined an integer titled "currentState", which holds a value which falls between 0 and 10. This is effectively the counter number, and is - thanks to Scott's wisdom and code - now linked to one of the ten (technically 9 because we don't count the first string) strings in the keymap. Again, when this value is changed (due to a user pressing a capacitive touch sensor), the currentState also changes accordingly; from here the code acknowledges the change and prints the new value and corresponding string.

At the bottom of the attached code is the crux of the process that I have discussed above (and that Scott added).

`#include <AnalogSmooth.h>

#include <CapacitiveSensor.h>

const int stringNumber = 10;

const String keyMap[stringNumber] = {"0", "this is an automated message ", "goodbye ", "call you later ", "see you later ",
":-) ", "add some text", "some more text", "etc", "more"
};

/*
CapitiveSense Library Demo Sketch
Paul Badger 2008
Uses a high value resistor e.g. 10 megohm between send pin and receive pin
Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
Receive pin is the sensor pin - try different amounts of foil/metal on this pin
Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/
int ledPin1 = 11;
int ledPin2 = 9;
int ledPin3 = 5;

CapacitiveSensor cs_12_10 = CapacitiveSensor(12, 10); // 1 megohm resistor between pins 12 & 10, pin 10 is sensor pin, add wire, foil
CapacitiveSensor cs_12_6 = CapacitiveSensor(12, 6); // 1 megohm resistor between pins 12 & 6, pin 6 is sensor pin, add wire, foil
CapacitiveSensor cs_12_3 = CapacitiveSensor(12, 3); // 1 megohm resistor between pins 12 & 3, pin 3 is sensor pin, add wire, foil

AnalogSmooth as = AnalogSmooth();

boolean fingerReleased = true;
boolean stageOneRight = false;
boolean stageTwoRight = false;
boolean stageThreeRight = false;

boolean stageOneLeft = false;
boolean stageTwoLeft = false;
boolean stageThreeLeft = false;

// unsigned long startTime; //the overall time coun
//unsigned long pressTime; //the timer which starts when a certain event (successful "stage" completion) is triggered

int counter = 0;
int currentState = 1;
int previousState = 0;

//________________________

const long longPressTime = 1000; //if sensor 1 or 3 is held for longer than 1000ms,
//decrease or inrease the counter values repetitively
unsigned long pressTime;
const unsigned long delayBeforeRepeat = 1000;
const unsigned long repeatTime = 500;

//__________________________________

void setup()
{

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

pinMode (ledPin1, OUTPUT);
pinMode (ledPin1, OUTPUT);
pinMode (ledPin1, OUTPUT); //none of these LEDs are being used atm.

cs_12_10.set_CS_AutocaL_Millis (30); //sensor calibration window
cs_12_6.set_CS_AutocaL_Millis (30); //sensor calibration window
cs_12_3.set_CS_AutocaL_Millis (30); //sensor calibration window

Serial.begin(9600);
}

void loop()
{

//startTime = millis();

long total1 = cs_12_10.capacitiveSensor(1);
long total2 = cs_12_6.capacitiveSensor(2);
long total3 = cs_12_3.capacitiveSensor(3);

float analogSmooth1 = total1;
float analogSmooth2 = total2;
float analogSmooth3 = total3;

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

/*
Serial.print(analogSmooth1); // print sensor output 1
Serial.print("\t");
Serial.print(analogSmooth2); // print sensor output 2
Serial.print("\t");
Serial.println(analogSmooth3); // print sensor output 3
*/

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

if ((total1 > 5) && (total3 < 7) && (stageOneLeft == false)) {
if (stageOneRight == false) {
stageOneRight = true;
}
//Serial.println("stage one complete RIGHT");
}

if ((total1 < 5) && (total2 > 4 ) && (total3 < 8) && (stageOneRight == true)) {
if (stageTwoRight == false) {
stageTwoRight = true;
}
//Serial.println("stage two complete RIGHT");

}

if ((total3 > 6 ) && (total1 < 5) && (stageTwoRight == true)) {
if (stageThreeRight == false) {
//Serial.println("stage three complete RIGHT");
stageThreeRight = true;
currentState++;

  if (currentState > stringNumber) {
    currentState = 1;
  }

  Serial.println("successful right swipe");
  stageOneRight = false;
  stageTwoRight = false;
  stageThreeRight = false;

  Serial.println(currentState);
  delay(150);
}

}

if ((total3 > 7 ) && (total1 < 6) && (stageOneRight == false)) {
if (stageOneLeft == false) {
stageOneLeft = true;
//Serial.println("stage one complete LEFT");
}
}

if ((total1 < 5) && (total2 > 4 ) && (total3 < 8) && (stageOneLeft == true)) {
if (stageTwoLeft == false) {
stageTwoLeft = true;
//Serial.println("stage two complete LEFT");
}
}

if ((total1 > 6 ) && (total3 < 7) && (stageTwoLeft == true)) {
if (stageThreeLeft == false) {
stageThreeLeft = true;
// Serial.println("stage three complete LEFT");

  currentState--;

  if (currentState <= 0) {
    currentState = stringNumber;
  }

  Serial.println ("successful left swipe");
  stageOneLeft = false;
  stageTwoLeft = false;
  stageThreeLeft = false;

  Serial.println(currentState);
  delay(150);
}

}

if (currentState != previousState) {
  displayString(currentState);
  previousState = currentState; //?
}

}

void displayString(int index) {
Serial.println(keyMap[index]);
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants