I have 5 capacitive sensors connected to my Arduino Mega2560, each with their own output.
When no sensors are touched, the serial monitor prints '00000'.
When the first sensor is touched, the serial monitor prints '10000'
When the second sensor is touched, the serial monitor prints '01000'
When the third sensor is touched, the serial monitor prints '00100'
and so on...
Then I send these numbers from my Mega2560 to my Pro Micro using RX, TX, and ground pins.
When the Pro Micro receives these numbers, they print on another serial monitor
Then, the Pro Micro will convert these numbers into variables. Then it uses these variables to move my mouse cursor using Mouse.move()
....This is the part I am having difficulty with... it seems the numbers (ex. 00010) are not being assigned to variables.
Here is the specific point I THINK the error is:
if (Q == 10000) {a = 1;} // sets inputs to arrays (var)
if (Q == 01000) {b = 1;}
if (Q == 00100) {c = 1;}
if (Q == 00010) {d = 1;}
if (Q == 00001) {e = 1;}
if (a == 1) {Mouse.move (100, 100); }
The code for my MEGA2560 and PRO MICRO are attached to this post
Also, the file for my MEGA is named "main'
I have looked at several examples, but I still don't understand how this would work, especially because I'm working with multiple different variables (ex. '00000, 00100, 00010).
because I'm working with multiple different variables (ex. '00000, 00100, 00010).
Those are not variables they are constants.
A variable is a name for a holder of information, like jar1, what is in that variable ( in the jar ) is a number.do not confuse the jar’s name with its contents.
Do not attach short code as many people like me are on mobile devices and they can’t handle .ino files.
if (Q == 10000) {a = 1;} // sets inputs to arrays (var) if (Q == 01000) {b = 1;} if (Q == 00100) {c = 1;} if (Q == 00010) {d = 1;} if (Q == 00001) {e = 1;}
ok, scratch that code.
Grumpy_Mike:
Those are not variables they are constants.
oops, you're correct.
TheMemberFormerlyKnownAsAWOL:
Q == 00010 Did you intend to express these values in octal?
Yes.
Each number represents whether or not my capacitive sensors are being touched, so if my first sensor is being touched, the first number will be '1'. If my second sensor is NOT being touched, then the second number will be '0'.. and so on.
Here is where I'm at: my Pro Micro is receiving a bunch of numbers (ex.00010), but I don't know how to store these numbers in variables. I think I need to convert string into variables, how do I do this?
There are 5 possible numbers my Pro Micro can receive, therefore I will need 5 variables (one for each).
OP's programs so others don't have to down load them
main.ino
#include <CapacitiveSensor.h>
/* upgrade resistor
* 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 give 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 a paper or plastic sheet.
*
* If you look at this YouTube video https://youtu.be/cFvh7qM6LdA?list=PL4QxnGNLxt7mmBF6EyDyeaNbrTCNEI7wB&t=196 you will see that the electrode strips have width and length,
* BUT a very small thickness. This means that the inter-electrode capacitance, that is the capacitance between side parallel electrodes is extremely small.
* If you use wires, the height and width will be the same and inter electrode capacitance will be a problem, especially if the wires are two or three wire diameters apart.
* Get a capacitance meter and make some POC models to measure the capacitance parameters.
*/
CapacitiveSensor S1 = CapacitiveSensor(3, 2);
CapacitiveSensor S2 = CapacitiveSensor(6, 5);
CapacitiveSensor S3 = CapacitiveSensor(9, 8);
CapacitiveSensor S4 = CapacitiveSensor(12, 11);
CapacitiveSensor S5 = CapacitiveSensor(15, 14);
int LEDpin1 = 4;
int LEDpin2 = 7;
int LEDpin3 = 10;
int LEDpin4 = 13;
int LEDpin5 = 16;
int ledArray[] = {1,2,3,4,5}; // array of all pin numbers that the LEDs are connected to
byte i = 0;
byte a = 0; // if a = 1, serial will print all (var) outputs
byte b = 1; // if b = 1, send data to pro micro
//---------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
while (!Serial)
{delay(3000); } // wait for serial port to connect
}
//---------------------------------------------------------------------------------------
void loop()
{
String readString;
String Q;
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if (isControl(c)) {
//'Serial.println("it's a control character");
break;}
readString += c; }} //makes the string readString
Q = readString;
//---------------------------------------------------------------------------------------
for(byte i; i <= 5; i++) { //set all led pins to output
pinMode(ledArray[i], OUTPUT);
}
long var1 = S1.capacitiveSensor(30); // to store capacitive sensor data
long var2 = S2.capacitiveSensor(30);
long var3 = S3.capacitiveSensor(30);
long var4 = S4.capacitiveSensor(30);
long var5 = S5.capacitiveSensor(30);
if (var1 >= 5) {var1 = 1;}
if (var2 >= 5) {var2 = 1;}
if (var3 >= 5) {var3 = 1;}
if (var4 >= 5) {var4 = 1;}
if (var5 >= 5) {var5 = 1;}
Serial.print(var1); // print sensor output 1
Serial.print(var2); // print sensor output 2
Serial.print(var3); // print sensor output 3
Serial.print(var4); // print sensor output 4
Serial.println(var5); // print sensor output 5
delay(3000); // delay to limit data to serial port
//---------------------------------------------------------------------------------------
}
pro_micro.ino
#include <Mouse.h>
#include <Keyboard.h>
int a = 0; // to store capacitive sensor data
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int Q = 0;
//---------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // Begin the Serial at 9600 Baud
while (!Serial)
{delay(3000); } // wait for serial port to connect
Serial1.begin(9600); // yes, this line is needed
while (!Serial1)
{delay(3000); } // wait for serial port to connect
}
//-------------------------------Check Serial Port---------------------------------------
void loop() {
String readString;
String Q;
while (Serial1.available()){
delay(1);
if(Serial1.available()>0){
char c = Serial1.read();
Serial.print(c);
if (isControl(c)){
break; }}}
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if (isControl(c)) { //'Serial.println("it's a control character");
break; }
readString += c; }} //makes the string readString
Q = readString;
//---------------------------------------------------------------------------------------
Mouse.begin();
Keyboard.begin();
if (Q == 10000) {a = 1;} // sets inputs to arrays (var)
if (Q == 01000) {b = 1;}
if (Q == 00100) {c = 1;}
if (Q == 00010) {d = 1;}
if (Q == 00001) {e = 1;}
if (a == 1) {Mouse.move (100, 100); }
delay(800);
}
Q == 00010 Did you intend to express these values in octal?
Yes.
I strongly suspect that your answer should have been "No"
From what I can see you have a string with the characters 00010 and you want to treat that as a binary number in which the second bit from the right is 1 and the rest are zero. 10 in binary is 2 in decimal
The compiler treats a number that starts with a zero as being to the base 8 (octal) so that 00010 would be 8 in decimal
I think the simplest solution is something like this (but I never use the String class so I may be wrong)
if (Q == "10000") {a = 1;}
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==