Where to include a digitalWrite message

Hi

Im adapting an example and trying to include a pinMode / digitalWrite message to send to a relay but I cannot find the correct position within the program to place the code. Ive used a simple relay code to check the hardware and it works with the board, but when I include the lines within my sketch it wont work. Any pointers/advice gratefully received! I have commented on the lines that im trying to include. TIA

// compiler error handling
#include “Compiler_Errors.h”

// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4

// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>

// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;

// mp3 behaviour defines
#define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.

// touch behaviour definitions
#define firstPin 0
#define lastPin 11

// sd card instantiation
SdFat sd;

void setup(){
Serial.begin(57600);

pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, OUTPUT); ///////////////////////THIS IS NOT WORKING///////////////////
pinMode(3, OUTPUT); ///////////////////////THIS IS NOT WORKING///////////////////

//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println(“Bare Conductive Proximity MP3 player”);

if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();

if(!MPR121.begin(MPR121_ADDR)) Serial.println(“error setting up MPR121”);
MPR121.setInterruptPin(MPR121_INT);

// Changes from Touch MP3

// this is the touch threshold - setting it low makes it more like a proximity trigger
// default value is 40 for touch
MPR121.setTouchThreshold(4);

// this is the release threshold - must ALWAYS be smaller than the touch threshold
// default value is 20 for touch
MPR121.setReleaseThreshold(2);

result = MP3player.begin();
MP3player.setVolume(10,10);

if(result != 0) {
Serial.print(“Error code: “);
Serial.print(result);
Serial.println(” when trying to start MP3 player”);
}

}

void loop(){
readTouchInputs();
}

void readTouchInputs(){
if(MPR121.touchStatusChanged()){

MPR121.updateTouchData();

// only make an action if we have one or fewer pins touched
// ignore multiple touches

if(MPR121.getNumTouches()<=1){
for (int i=0; i < 1; i++){ // Check which electrodes were pressed
if(MPR121.isNewTouch(i)){

//pin i was just touched
Serial.print(“pin “);
Serial.print(i);
Serial.println(” was just touched”);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(2, HIGH); ///////////////////////THIS IS NOT WORKING///////////////////
digitalWrite(3, HIGH); ///////////////////////THIS IS NOT WORKING///////////////////

if(i<=lastPin && i>=firstPin){
if(MP3player.isPlaying()){

} else {
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
}else{
if(MPR121.isNewRelease(i)){
MP3player.stopTrack();
Serial.print(“pin “);
Serial.print(i);
Serial.println(” is no longer being touched”);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(2, LOW); ///////////////////////THIS IS NOT WORKING///////////////////
digitalWrite(3, LOW); ///////////////////////THIS IS NOT WORKING///////////////////
}
}
}
}
}
}

BUT…

This works fine…

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(1000);
}

What I am doing wrong!!?

Are you running the sketch and the test sketch on the same hardware? Same shield is attached? What about the built in LED? Is it indicating properly? Is it possible the shield uses pin 2 or 3? Can you please provide a link to the shield?

One thing you are doing wrong, is posting code without code tags.

This is a bit odd:

for (int i=0; i < 1; i++){

Are you sure pins 2 and 3 aren’t used by the shield for some purpose?
This one right?

Bingo

aarg:
Are you running the sketch and the test sketch on the same hardware? Same shield is attached? What about the built in LED? Is it indicating properly? Is it possible the shield uses pin 2 or 3? Can you please provide a link to the shield?

One thing you are doing wrong, is posting code without code tags.

Hi, sorry I am just learning also how to use this forum. Im not sure what a code tag is.

Yes the builtin LED is indicating properly, the relay is not a shield, it is independent to the board. The simple test sketch works perfectly, then without changing any connections the relay does not change with the main sketch. But the leds on the relay board do light up so it is receiving a signal.

The relay is a...

You have a pin conflict on 2 and 3. Instructions for using code tags are in the sticky threads at the top of the forum.

aarg:
You have a pin conflict on 2 and 3. Instructions for using code tags are in the sticky threads at the top of the forum.

Ahaaa... Indeed you are right, thank you. I thought as the relay was not a shield this would not be an issue. Wrong.

But now I have a new problem! The relay is constantly flicking while the electrode is pressed, where as I want it to go to high while it is pressed, then go to low when it is released.

Sorry for my terminology everyone, very new to this.

kilmo:
But now I have a new problem! The relay is constantly flicking while the electrode is pressed, where as I want it to go to high while it is pressed, then go to low when it is released.

So which pins do you have it connect to now?
Did I get a picture of the right shield then?
“Electrode is pressed” - what does that mean?
Does the sketch work without the shield, but with the new pin assignments - whatever they are?

No the relay is a...

I am now using pin 4.

On this board there are 12 'electrodes'... Inputs for connecting sensors to...

Sorry Pin 5.

But relay constantly triggers on and off rather than just flicking to on while touched then off on release.

kilmo:
No the relay is a...
https://www.amazon.co.uk/gp/product/B078Q326KT/ref=ppx_yo_dt_b_asin_title_o02_s01?ie=UTF8&psc=1

No, do we have the correct MP3 player shield? We need to check the shield doesn’t use the pin you are using for the relay.
The SparkFun one doesn’t use pin 5. Bit we don’t know if that is the one you are using or not
Maybe your shield uses pin 5 as well....

GOT IT!!

Thank you so so much! Yes the pin was conflicted, but I was confused as its not a shield as such, the mp3 player is built in to the board, but on closer inspection there are pins with no function next to them and ive found one that does it!

Thank you!!