simple touch-initiated fade code inquiry

Hi there! Still terribly unfamiliar with Arduino, but would like some help with a simple project to better my understanding. I'm trying to code a touch sensor-activated light that, instead of turning on immediately, fades on and fades off when the hand leaves the sensor. I've so far figured out the on/off part:

const int analogInPin = A0;  
const int analogOutPin = 9;

int sensorValue = 0;        
int fadeValue = 0;

void setup() {
  pinMode(analogOutPin, OUTPUT);
}

void loop(){
  sensorValue = analogRead(analogInPin);
  analogWrite(analogOutPin, fadeValue);
  
  if(sensorValue > 50)
  {
    digitalWrite(analogOutPin, HIGH);
  }
  else
  {
    digitalWrite(analogOutPin, LOW);
    }
  }

I've been trying to incorporate the example fade code into mine but to no avail. I'd really appreciate any and all advice/help!

alittlebigadventure

Please show us your attempt to incorporate the fade code.

hi,

here was my attempt for the fade code. I tried to put the for loop code into my touch code but it doesn't work that well.

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;
int fadeValue = 0;

void setup() {
pinMode(analogOutPin, OUTPUT); // declare LED as output
}

void loop(){
sensorValue = analogRead(analogInPin);
analogWrite(analogOutPin, fadeValue);

if(sensorValue < 50)
{
analogWrite(analogOutPin, LOW);
}
if(sensorValue > 50)
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 2) {
analogWrite(analogOutPin, fadeValue);
//digitalWrite(analogOutPin, HIGH);
delay(30);
}
}

Are you trying to implement a touch-on/touch-off kind of functionality? That would require a state change detection. Right now the output just tracks the input. You need to specify what you expect, and what you are seeing. "Doesn't work that well" doesn't help us understand your problem.

There are four states - off, on, fading up and fading down. Work out what should trigger
transitions between them and when. For instance do you want to respond to input during
a fade or let the fade complete? Draw out all the possibilities and make them explicit in
your thinking.

Following on from MarkT. I like to set global variables like...

enum states {TS_ON, TS_FADING_OFF, TS_OFF, TS_FADING_ON} switchState = TS_OFF

and then in loop you can use a switch statement like....

void loop() {
  int sensorValue=analogRead(analogInPin);

  switch (switchState){
    case TS_ON:
      if (sensorValue>50){
        //set switchState=TS_FADING_OFF
        //do stuff to start fading off        
      }
      break;
    case TS_FADING_OFF:
      //fad off 
      //when off set switchState=TS_OFF
      break;
    case TS_OFF:
      if (sensorValue>50){
        //set switchState=TS_FADING_ON
        //do stuff to start fading on
      }
      break;
    case TS_FADING_ON:
      //fad on
      //when on set switchState=TS_ON
      break;
  }

}