0

I have a simple sketch that tests input from 2 touch sensors and outputs to 2 relays (if a sensor is touched, trigger the respective momentary relay). It has been a bit quirky to say the least. perhaps I need to adjust delays or adjust sensitivity of the touch sensors, but I feel that maybe the coding could be more efficient. Would using a WHILE or DOWHILE loop work better than an IF statement for this? It seems like there is a timing issue between the relay and the sensor. When I hit the sensor, the relay triggers immediately, but then it doesn't shut off right away. Also if both sensors are made at the same time, I am seeing only 1 relay trigger or neither. I am trying to eliminate the code as my failure point. I have the code printing to the serial monitor to show "Sensor 1 touched" or "Sensor 2 touched", and sometimes that triggers once and sometimes it will print multiple times.

int touchPin1 = 2; //Touch sensor 1
int touchPin2 = 4; //Touch sensor 2
int val_1 = 0; 
int val_2 = 0; 
int relayPin1 = 8; //Relay 1
int relayPin2 = 9; //Relay 2

void setup() 
{
  Serial.begin(9600);
  pinMode(touchPin1, INPUT); 
  pinMode(touchPin2, INPUT);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);

}

void loop() {
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);
  val_1 = digitalRead(touchPin1);
  val_2 = digitalRead(touchPin2);  
  if(val_1 ==1)
  {
  Serial.println("Pad 1 Touched");
  digitalWrite(relayPin1, HIGH);
  }
  if(val_2 ==1)
  {
  Serial.println("Pad 2 Touched");
  digitalWrite(relayPin2, HIGH);
  }
  delay(100);
  //Serial.println();
}

Basically I am asking if there is a more efficient way to code this.

Touch Sensors: Adafruit Standalone Momentary Capacitive Touch Sensor Breakout Adafruit Part Number: 1374

Relays: SunFounder Relay Module for Arduino and Raspberry Pi 5V DC Trigger by HIGHLO (HIGH Trigger)

7
  • 4
    As we do not have a crystal ball, we cannot help you debug code we do not see. Commented Apr 27, 2018 at 17:08
  • For us to be able to help us you will have to share your code. We can't recommend one strategy over another when we don't know what it is your code is doing, or even what sensors you are using and how the code for them works. You will have to add your code, along with a drawing of your setup (known as a schematic) or at the very least a photograph of your breadboard or whatever you're using, to your question (use the edit button under your question).
    – Majenko
    Commented Apr 27, 2018 at 18:25
  • What do you want the beahviour to be? As long as the touch pad is pressed, the relay is activated, and as soon as you don't touch it anymore the relay closes? Because I don't understand why you always digitalWrite(relayPin1, LOW); in every loop iteration, and then (depending on val_1) immediately set it HIGH again. Commented Apr 27, 2018 at 21:34
  • As you stated, I would like the touch pad to activate the relay as long as the pad is touched and then off when no longer touched. Ideally, I would like any of the relays to only be able to activate for a max of 1 second and not be able to be activated again for 1 second. Commented Apr 27, 2018 at 22:54
  • that is not what your code does ..... the loop() repeats 10 times a second because of the delay at the end .... look at your code carefully and determine what each line of your code does .... you will see that it does not do what you think it does
    – jsotola
    Commented Apr 28, 2018 at 1:31

1 Answer 1

2

What you are lacking is the else concept:

  • If a touch pad is pressed, then
    • Turn the relay on
  • else
    • Turn the relay off

Rather than turn the relay off every iteration, you only turn it off if the touch pad is not pressed.

That can be written as:

val_1 = digitalRead(touchPin1);

if(val_1 ==1)
{
  Serial.println("Pad 1 Touched");
  digitalWrite(relayPin1, HIGH);
} else {
  digitalWrite(relayPin1, LOW);
}

If you want your messages to be more meaningful and not spam, then you can remember the previous state and only do things if the state changes:

static uint8_t oldVal1 = LOW;

uint8_t val1 = digitaRead(touchPin1);

if (val1 != oldVal1) {
    oldVal1 = val1;
    if (val1 == HIGH) {
        Serial.println("Pad 1 Touched");
        digitalWrite(relayPin1, HIGH);
    } else {
        Serial.println("Pad 1 Released");
        digitalWrite(relayPin1, LOW);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.