I am new to coding, so please provide lots of details. I am working on creating a touch-screen using capacitive sensors. I will create a grid pattern using wires, with capacitive sensors on the ends of these wires. The idea is that by touching points on this screen where the wires overlap, two or more wires will be touched, which will eventually tell my mainboard (Arduino Mega2560) where to move the mouse curser. I have not set up the mouse movements yet, but I will do that later.
Right now each wire is controlled separately, and because I plan on adding hundreds of these wires in the future, the amount of code I would need would be insane. How do I compact my code as much as I can?
Side note: I will be using an I/O expander so I have enough space for all these wires
Also, the 'X' and 'Y' coordinates are not being displayed on the serial monitor as intended, how do I fix that?
then create a sub-function with appropriate arguments for processing the touch switches and update the LEDs and generate serial output
looks like you use pairs of touch switches in each, so you may sequence thru each touch switch to update a read value used in above
Algorithms + Data Structure = Programs suggests organizing your data appropriately so that a simple algorithm can do what you need on a large set of data
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance
And How to use this forum
Especially item #7 on posting code.
If you post your code in code tags more people will see it and you'll get better, more helpful answers.
I don't know specifically what code you need, but from your description you will be doing the same thing over and over. The general idea is to create a function that does what you need then call it while passing appropriate variables that tell it what to do, in this case variables for which wire to read.
My observation is that what you are trying to do won't be easy, and given your level of knowledge I think you are in for a steep learning curve.
WhiteFalls:
Right now each wire is controlled separately, and because I plan on adding hundreds of these wires in the future, the amount of code I would need would be insane. How do I compact my code as much as I can?
I have not looked at your program because I don't fancy wading through 29k of code.
How could your program possibly have got that large before you became aware of this problem? I suggest you write a short program to explore this one aspect of the problem and then it will be easier to help you.
My wild guess is that you need to learn about arrays
You don't need to tell the complier how many elements are in the array as you are initilising it with 7 elements, so it already knows.
for(int i; i <= 7; i++) {
You have 7 elements in your array, how many will that for loop try to access? (Hint, it's not 7).
[Edit] AWOL beat me to it and he noticed a different problem to the problem I noticed.
I'm not an expert on this but you may be able to do something like this
EDIT - see below - this is not correct, but as I write this I am not sure what is
TheMemberFormerlyKnownAsAWOL:
I'd rather hoped that you'd spot that an array with zero elements is a pretty pointless thing, and rethink.
Fair point - I had missed that. However I remain of the view that this Thread is for the OP's benefit (not mine) and you missed an opportunity to help him/her.
Also, I imagined I had a more serious mistake. Will this work?
WhiteFalls:
ok, I was able to place my led pin modes into an array:
int ledArray[7] = {4, 7, 10, 13, 16, 19, 22};
void setup() {
Serial.begin(9600);
}
void loop()
{
//set all led pins to output
for(int i; i <= 7; i++) {
pinMode(ledArray[i], OUTPUT);
}
}
How do I place this into an array? Is there a way to compact it?
CapacitiveSensor touchSwitch1 = CapacitiveSensor(3, 2);
CapacitiveSensor touchSwitch2 = CapacitiveSensor(6, 5);
CapacitiveSensor touchSwitch3 = CapacitiveSensor(9, 8);
CapacitiveSensor touchSwitch4 = CapacitiveSensor(12, 11);
CapacitiveSensor touchSwitch5 = CapacitiveSensor(15, 14);
Want tighter Arduino code? Never use an int for what a byte can hold.
8 bit byte can hold 0 to 255. 16 bit int can hold -32768 to +32767.
You won't get a pin number as big as 255 so don't waste 16 bits on an int where an 8 bit byte is plenty.
Ditto with the for-next index that counts from 0 to 6, a byte will do fine.
If you need small - to + values, a char can hold -128 to +127.
I dunno the CapacitiveSensor class initializers so... structs and class objects ARE variables, CAN be used in arrays.
CapacitiveSensor touchSwitch[ 5 ]; // creates an array of CapacitiveSensor objects.
You can fill in the values inside of setup().
Using that array you won't have to have separate code for each switch.
You can use the F() macro with Serial.print() and println() to keep the text in flash, not RAM where it otherwise loads.
You can use PROGMEM to put data that never changes, constants and constant text, into flash instead of RAM.
I am working on creating a touch-screen using capacitive sensors. I will create a grid pattern using wires, with capacitive sensors on the ends of these wires. The idea is that by touching points on this screen where the wires overlap, two or more wires will be touched, which will eventually tell my mainboard (Arduino Mega2560) where to move the mouse curser.
If you are relying on two crossed wires contacting each other, why are you complicating the situation with adding capacitive touch?
WhiteFalls:
I am working on creating a touch-screen using capacitive sensors. I will create a grid pattern using wires, with capacitive sensors on the ends of these wires. The idea is that by touching points on this screen where the wires overlap, two or more wires will be touched,
The capacitive sensors would play no real part, you're looking at wires making contact (a form of switch) which would use very different code than cap sense.
Look up how membrane keyboards work. I don't want to type an hour to tell less than a web search can turn up.
TomGeorge:
Hi,
If you are relying on two crossed wires contacting each other, why are you complicating the situation with adding capacitive touch?
No, the wires will not contact each other. There will be a very thin layer of material in between, and if done correctly it will create a small electromagnetic field.
The following explains this (I just found it from Google).
An electromagnetic field (also EM field) is a classical (i.e. non-quantum) field produced by moving electric charges.
The electric field is produced by stationary charges, and the magnetic field by moving charges (currents)
It sounds complicated, but surprisingly, it's not that bad. If you look into capacitive touch screens, you may get a better understanding. Here is a YouTube link that really helped me understand (start watching the video at 3:16).
Also, I was able to compact my code a lot! Thanks for all the feedback.
it will be very difficult to set everything up properly, but I definitely want to do this. And yes, I understand I could just buy one from eBay (I am going to make my own, as I would like to start a business based on this).
Robin2:
How could your program possibly have got that large before you became aware of this problem? I suggest you write a short program to explore this one aspect of the problem and then it will be easier to help you.
...R
I knew it was going to be big, but I didn't know what else to do at the time. It's not a big deal for me, as I set up a simple script to copy and paste a whole bunch of stuff for me. But anyways, your right, I definitely need to do something else, because coding every single possible point on the touch screen will take WAY too long, nevermind running out of storage space on my Arduino board.
Instead, I'm thinking that I would use triangulation to determine the point of which someone touches the screen. So if 2 wires (or the diamond grid pattern as explained in the video link) were touched, I would use triangulation to determine the location(s) that were touched. So my question now is; would this even be possible? if so, how should I do that?
Side note: once I'm able to somehow get this to work, I want to move the mouse cursor on my computer in relation to the touch screen, but that is a completely different topic.
Hi,
If you look at that YouTube video, you will see that the electrode strips have width and length, BUT a relatively 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.
I would suggest you get a capacitance meter and make some POC models to measure the capacitance parameters of your project.
void loop()
{
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);
}
ok, I would like to continue compacting some code. (see code above) Here is what I have, but once it is fully finished I will probably have about 50 of these lines, which is a lot, so I want to compact it.
(see code below) I am trying to replace the numbers with an int (except for 30), and this int would increase by one number each time.
Therefore, in theory, 'int i' should be replaced with the number '1' then you would have 'long var1 = S1.capacitiveSensor(30);'
Then 'i' would be increased by one number, and this would repeat until 'i' is at a certain number (maybe 50)
TomGeorge:
If you look at that YouTube video, you will see that the electrode strips have width and length, BUT a relatively 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.
I would suggest you get a capacitance meter and make some POC models to measure the capacitance parameters of your project.
{
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);
}
ok, I would like to continue compacting some code. (see code above) Here is what I have, but once it is fully finished I will probably have about 50 of these lines, which is a lot, so I want to compact it.