Hello! I'm new to the community (and am a novice coder) and am currently working on my Graphic Design thesis project involving the Arduino. Specifically, I'm using the Sparkfun RedBoard.
CONTEXT
I'm currently doing a trial setup of a capacitive proximity sensor and I have an LED connected that I want to have light up when an item is close enough to trigger the sensor. I'm slowly figuring things out and am combining knowledge and code from various tutorials and projects that I've found and/or tried. In my first attempt at the coding, I was successful at getting the capSensor to respond as desired and the serial monitor was showing correct inputs. I tried adding a LED dim/bright code (triggered by the sensor values) by mapping and constraining the inputs into a different output for the LED. The results were... off. My serial monitor was showing negative values for the sensor reading and it was in the 2000s. Then, I decided that I didn't really need different levels of brightness in the indicator anyway, what I really wanted was just a simple on/off LED effect (because I only really need those two values for the application of the sensor anyway). This is where I ran into my current problem.
PROBLEM
While attempting to code a Capacitive Proximity Sensor to trigger an LED on/off, I received the following error code.
In function 'void loop()':
CapSense-LED:24: error: 'lightState' was not declared in this scope
lightState = digitalRead(sensorPin); //returns high or low value from sensor
^
exit status 1
'lightState' was not declared in this scope
MY CODE
I'm pretty sure I've declared it. I've also tried moving it to void setup & changing it into a global variable, but when I changed it to global variable... it gave me an error code saying that I didn't declare a "type".
#include <CapacitiveSensor.h>
/*
CapitiveSense Library Demo Sketch
Paul Badger 2008
Uses a high value resistor e.g. 10M between send pin and receive pin
Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
Receive pin is the sensor pin - try different amounts of foil/metal on this pin
*/
const int ledPin = 9;
const int sensorPin = 2; //the pin that reads the capSensor value as Hight or Low
CapacitiveSensor cs_2_4 = CapacitiveSensor(2, 4); // 10M resistor between pins 2 & 4, pin 4 is sensor pin, add a wire and or foil if desired
//int lightLevel, high = 0, low = 1023;
void setup()
{
//cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //sets LED pinMode
pinMode(sensorPin, INPUT); //sets sensor pinMode
}
void loop() {
lightState = digitalRead(sensorPin); //returns high or low value from sensor
digitalWrite(ledPin, lightState); //writes a high or low value for LED light
long start = millis();
long total1 = cs_2_4.capacitiveSensor(30);
//long total2 = cs_4_6.capacitiveSensor(30);
//long total3 = cs_4_8.capacitiveSensor(30);
Serial.print(millis() - start); // check on performance in milliseconds
Serial.println("\t"); // tab character for debug windown spacing
Serial.print(total1); // print sensor output 1
Serial.print("\t");
Serial.print(lightState);
delay(700); // arbitrary delay to limit data to serial port
//mapping(); //mapping values of capSense for analog LED
}
/*void mapping() {
//lightLevel = map(lightLevel, 0, 500, 0, 255);
//lightLevel = constrain(lightLevel, 0, 255);
if (lightLevel < low) {
low = lightLevel;
}
if (lightLevel > high) {
high = lightLevel;
}
lightLevel = map(lightLevel, low+30, high-30, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
*/
Please help and thanks in advance!
(Also, sorry if this is lengthy, I wanted to be as thorough as possible.)
P.S. I'm attaching a pic of my board in case that matters at all or if you have any additional tips. (Note: I had to connect five 10K resistors together because I didn't have any larger resistors. I don't know if the LED resistor/connection could potentially affect the rest of the circuit or not.)