Input's/Output's not registering, Simple problem with code?

Hello!,

I’m quite new to the Spark Core and installed an LED to a capacitative touch sensor I bought from adafruit (https://www.adafruit.com/product/1362). I had everything wired up correctly beforehand when I tested it on a raspberry pi but using the spark core nothing really is registering. Is my code broken? When you touch the sensor connected to D1 it should light up the LED.

Also another quick question, how would I be able to view where the information from Serial.println goes?

Thanks

int led = D0;  
int touch = D1;

int pressed;

void setup() {

  pinMode(led, OUTPUT);
  pinMode(touch, INPUT);
  Serial.begin(9600);
 }

void loop() {
 
 pressed = digitalRead(led);
 
 digitalWrite(led, HIGH);
 
 if (pressed == HIGH) {
     
     Serial.println("LED is on");
     digitalWrite(led, HIGH);
     
     delay(1);
     
 }
 
 else {
     
     Serial.println("LED is off");
     digitalWrite(led, LOW);
     
     delay(1);
     
 }
 
 }

I cant see anything wrong with the code at a very quick glance, except maybe to turn on a pullup or pull down on the input?

You could try the inbuilt LED its on D7, that eliminates one variable.

There is a good little trick to opening up the serial port here, 2nd post!

There is a digitalWrite(led, HIGH) inside your main loop. This will keep the led high unless you touch the button. Try removing that line, and see what it does, it’s not useful anyway.
Well, the main loop happens so fast, that the led will always seem to be on, regardless of the input.

Apart from that, you shouldn’t check for a HIGH. According to your adafruit link:
“When a capacitive load is detected (e.g. a person touches one of the conductive contacts) the corresponding LED on the right lights up and the output pin goes low.”

So if you touch the thing, it should go LOW, which means it’s normally always HIGH - the LED is always on, and should only turn off when you touch it.
Due to the above mentioned digitalWrite, you’ll never see this, since it happens too quickly.

So, try removing the first digitalWrite, and check for a LOW state. See if that works.

For the Serial info, a quick forum search should suffice :wink:

Good luck!

1 Like

@JustSomeGuy, @Moors7 is partially right ( :stuck_out_tongue: ). You declare “led” as an output in setup() but use it as an input in loop() with"

   pressed = digitalRead(led);

So “pressed” will never have the correct value. So the line should be:

  pressed = digitalRead(touch);

From what I could find on the specs, the output of the sensor is HIGH when touched so your logic is correct. However, like @Moors7 indicated, you need to remove the first digitalWrite(led, HIGH). The rPi example actually shows a 0.1sec delay which is 100ms. That delay can be placed after the if/else since it is applied in both if and else. So, the code in loop() should be:

void loop() {
	pressed = digitalRead(touch);

	if (pressed == HIGH) {
		Serial.println("LED is on");
		digitalWrite(led, HIGH);
	}
	else {
		Serial.println("LED is off");
		digitalWrite(led, LOW);
	}
	delay(100);	// The rPi code delays for 100ms
}

:smile:

3 Likes

Yeah, that might be kind of problematic ;p (stupid phone makes it easier to miss things :’( )
So, all things considered; read, write and the state, need to be edited.

2 Likes

Everything looks good here… just wanted to say you can actually use a digitalRead() on an OUTPUT pin though… some people in Arduino land like to save the state of their output in the microcontroller’s output data latch (i.e. on the port pin hardware) instead of in a variable. So something like this will toggle an LED:

digitalWrite(LED_PIN, !digitalRead(LED_PIN) ); // toggle the LED_PIN

It’s ugly, but technically does the job.

Not what you intended in your code though, just thought I’d mention it :wink:

3 Likes

Thank you everyone!

It was late at night so I suppose those two things just flew over my head. I fixed it and it’s all working now. I normally work in python and so going back to C is different. And also, thank you BDub, that will come in handy later!

Cheers,

Hashim C