Hello!
I've been using the arduino starter box and its been great. Just done the project 12 touch sensor lamp and everything worked fine, I've tried adding 2 more LEDs to try to light up as like a little challenge*(im using RED, YELLOW and BLUE LEDs in that order)* .
Everytime I press the foil, only 2 out of 3 LEDs light up, 3rd one dosen't want to light up, even tho there is enough threshold to light up.
I've also tried switching them around but when i put BLUE led first the threshold doesn't get loaded at all and when I put YELLOW one threshold goes up but only a little to like 140 . I also tried using only RED LEDs but that also doesn't seem to work. Threshold just stays 0 all the time. Also tried removing resistors one by one and that didn't make a lot of difference too.
My question is: Does this happen because of LEDs resistance or is there another reason for it (me not connecting it right or bad programing)?
Also here is the code im using and a picture of the sheme:
#include <Arduino_CapacitiveTouch.h>
const int TouchPin = 2; //Touch sensor
const int LEDR = 12; // Red LED
const int LEDY = 8; // Yellow LED
const int LEDB = 7; // Blue LED
int ThresholdValue = 6000;
CapacitiveTouch touchSensor(TouchPin);
void setup() {
Serial.begin(9600);
pinMode(LEDR, OUTPUT);
pinMode(LEDY, OUTPUT);
pinMode(LEDB, OUTPUT);
touchSensor.begin();
touchSensor.setThreshold(ThresholdValue);
Serial.print("Threshold set to: ");
Serial.println(touchSensor.getThreshold());
}
void loop() {
int SensorValue = touchSensor.read();
Serial.println(SensorValue);
if(SensorValue >= 2000 && SensorValue <=4000 ){
Serial.println("Osjeti se dodir"); //means I can feel the touch
digitalWrite(LEDR, HIGH);
}
if(SensorValue >= 4000 && SensorValue <=8000 ){
Serial.println("Osjeti se dodir"); //means I can feel the touch
digitalWrite(LEDY, HIGH);
}
if(SensorValue >= 8000 && SensorValue <=10000 ){
Serial.println("Osjeti se dodir"); //means I can feel the touch
digitalWrite(LEDB, HIGH);
}
if(SensorValue <2000){
Serial.println("neosjeti se dodir"); //means I can't feel the touch
digitalWrite(LEDR, LOW);
digitalWrite(LEDY, LOW);
digitalWrite(LEDB, LOW);
}
delay(10);
}
If you have any solutions for me that would be great!
Thank you in advance
SOLVED
for some odd reason pin 7 just doesn't work with this sheme. Everything else works with pin7 including the blink test and light up test just this doesn't work.
Thanks to everyone who helped!
Can you try by replacing “ int SensorValue = ” with “ long SensorValue = “ ?
On the Arduino UNO (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
You are comparing SensorValue with 100.000, which is more than SensorValue could ever contain. A “long” is a 4-byte field, so can hold 100.000
A circuit diagram with actual values would help. Do you have access to a multlimeter?
Blue LEDs need more voltage to operate. If you have a meter with a diode function, it should show the forward voltage of the LED.
Try going back to a single blue LED. It should be fine. Check the actual pin output voltages.
Do the calculations for the LED resistor values taking into account their forward voltages and the pin voltage.
It's always safer to use a separate power supply for driven loads, but generally low power LEDs are fine.
You could try the Blink example using a different pin number to the one in the example.
DNMS, your if structures are mutually exclusive(other than sloppy overlaps), if the LEDs are illuminated when high, only one should ever light.
This is why we ask for a schematic, I'm not going to try and determine where the LED cathode is by peering at the picture of the LEDs on the breadboard.
I've also tried switching them around but when i put BLUE led first the threshold doesn't get loaded at all and when I put YELLOW one threshold goes up but only a little to like 140 . I also tried using only RED LEDs but that also doesn't seem to work. Threshold just stays 0 all the time. Also tried removing resistors one by one and that didn't make a lot of difference too.
No idea why you think LED order would make any difference to capacitive sensor response, so please reword to make your intent clearer.
You can verify each LED working by putting each pin into the Blink.ino example.
edit - I see your GND is your LED common, so each LED will be on when it's output is high. Again, verify function with Blink, to ensure you've not blown an output - which can easily happen if you 'remove resistances' by jumpering them.
Your 100000 threshold was probably a typo, should have been 10000 - please confirm.
I don’t have one :(, but It could be a reason maby I’ll try hooking up 5v and try then when I come home or try to get forvard voltage calculations, Thanks!
LEDs are not fryed 100% I’ve attempted to put them alone and they are working perfectly :)
I don’t think LED order matters, I’ve observed that putting diffrend LEDs Blue Yellow Red gives me diffrent threshold values. Im sorry im completly new to this. LEDs are connected correctly + is connected to pins - is connected to ressistors and GND, and yesh 100.000 is a typo sorry. Im currently not at home but Ill try what you said when I come home thanks!
Your threshold value is a constant in your code, so I doubt it's changing.
This you have not addressed - it is very possible to damage an Arduino output, if you wire from output - LED - GND with no series resistance, which I thought you implied in your earlier text. If an output is damaged it is likely that you won't see an LED light, even if the LED itself is functional. Hence I'm suggesting you try your output-resistor-LED-GND circuits one at a time.
The resistance or order of the LEDs has nothing to do with the touch sensor values returned by touchSensor.read(). They will either light up when the corresponding pin GPIO goes high or not when it is low. Can I ask, what resistor values have you used in series with the LEDs? Are they all 1k?
(@tuna one reason why a circuit diagram is helpful. Difficult to tell from the photo.)
Erratic sensor readings are just as likely to be caused by an intermittent connection between the wire and the foil.
Incidentally, looking at the Arduino_CapacitveTouch library source, it appears that the function touchSensor.setThreshold() has no relevance when reading raw sensor values with touchSensor.read(). It only comes into play when touchSensor.isTouched() is called, where any reading above the threshold is considered true otherwise its false. I guess the line was just copied or retained from an example but touchSensor.isTouched() is not actually used in the presented sketch.
If this were an assignment (SensorValue = 100000) then it would, of course, overflow int SensorValue. But since it is a comparison to a literal value and since the highest value of an int type can't be more than 32767, then presumably this comparison will always be true, so the condition would be true for any value >= 8000. @tuna has now confirmed its a typo and a value of 10,000 will provide the intended range.
I've figured it out!
For some reason pin 7 is the problem? When i changed it to pin 4 worked perfectly fine.
I've also checked if pin 7 is fried but no seems to be working just fine.
Thanks for helping tho!
I've figured it out!
For some reason pin 7 is the problem? When i changed it to pin 4 worked perfectly fine.
I've also checked if pin 7 is fried but no seems to be working just fine.
Thanks for helping tho!!!