Third sensor won't register in code...

I have a (DFRobot brand) touch sensor, vibration sensor, magnet sensor and a RGB Led hooked up to a board.

The code runs fine with the touch and vibration sensors (activating the RGB Led to the appropriate colour). All I want the Magnet Sensor to do (for the time being) is activate the onboard LED, which it won't.

I have tested the sensor independantly of everything else and it works fine (activates the onboard LED), I have then copy/pasted that code in (like I have for all the other sensors) and it won't register (though the red light on the magnet sensor indicates it does have power and is picking up the magnet - but the signal isn't being picked up by the arduino anymore).

Here is my code (which i'm sure is the problem)...

#define SensorLED 13  //on-board LED assigned to pin 13
#define VibSensorINPUT 3  //vibration sensor on pin 3 (must be ~3 pin)
#include <Wire.h>  //magnet sensor library

unsigned char state = 0;  //vibration sensor check state

int RGBred = 5;  //connect red LED pin to 5 pin
int RGBgreen = 6;  //connect green LED pin to 6 pin
int RGBblue = 7;  //connect blue LED pin to 7 pin

int TouchSensorINPUT = 4;  //connect Touch sensor on pin 4

int MagSensorINPUT = 2; //set pin 2 for magnet sensor signal wire
int MagVal;  //magnet 'on' value

void setup() {
  
pinMode(SensorLED, OUTPUT);  //sensor detect triggers onboard LED
pinMode(VibSensorINPUT, INPUT);  
attachInterrupt(1, blink, FALLING); //trigger the blink function when vibration sensor moves

pinMode(RGBred, OUTPUT);  //assign pin 5 to red RGB LED
pinMode(RGBgreen, OUTPUT);  //assign pin 6 to red RGB LED
pinMode(RGBblue, OUTPUT);  //assign pin 7 to red RGB LED

pinMode(TouchSensorINPUT, INPUT);  //set touch sensor pin to input mode

Serial.begin(9600);  //magnet sensor detect code
Serial.println("magnet:");
pinMode(MagSensorINPUT,INPUT); //assigns MagSensorINPUT to pin 2

}


void loop() {

if (state != 0) {  //vibration sensor detect code
  state = 0;
  digitalWrite(SensorLED, HIGH);
  digitalWrite(RGBred, HIGH); //RGB LED test code
  delay(1000); //red eyes stay on for 1 second
  } 
else {
  digitalWrite(SensorLED, LOW);  //vibration sensor detect code
  digitalWrite(RGBred, LOW); }

    if (digitalRead (TouchSensorINPUT) == HIGH)  //touch sensor detect code
    { 
    digitalWrite(SensorLED, HIGH);
    digitalWrite(RGBgreen, HIGH);
    delay(1000);
    }
    
    else {
    digitalWrite(SensorLED, LOW);  //touch sensor detect code
    digitalWrite(RGBgreen, LOW);
    }

  MagVal = digitalRead(MagSensorINPUT);  //magnet sensor detect code
  Serial.println(MagVal);
  if (MagVal==1) digitalWrite(SensorLED, HIGH);
  else digitalWrite(SensorLED, LOW);  //magnet sensor detect code

}
  

void blink() {  //interrupt function for vibration sensor 
    state++;
}

From all my experimenting, if I delete the IF / ELSE statements for the other two sensors from the loop - the magnet works, so that portion of code is interfering or something?

I have double and triple checked the wiring, as well as re-uploading the stand alone code for each sensor and testing it (without altering the wiring for the full setup) and everything works fine (including magnet sensor)

Also, i'm a absoloute beginner arduino user - STATE programming is well beyond me at this point (and I have time restrictions, since this is part of a class). So a veeeeeery simple solution would be appreciated =P

Curious, if(MagVal == 1) .... Should that not be if(MagVal == HIGH)?

Every time there is motion, you turn the SensorLED and RGBred pins on, and bury your head in the sand for 1 full second.

During that time, you don't read the magnet sensor.

Every time the touch sensor is touched, you turn the SensorLED and RGBgreen pins on, and bury your head in the sand for 1 full second.

During that time, you don't read the magnet sensor.

Could that be why the magnet sensor doesn't seem to work? Is the magnet sensor pin always pulled HIGH or pulled LOW? Or, do have a floating pin problem when there is no magnet nearby?

A schematic, instead of handwaving, is in order.

By the way, your code indenting sucks. Use Tools + Auto Format, put every { on a line by itself, and get rid of the

useless white

space in your code.

Yes, while the red or blue RGB states are active, they stay active for 1 second (they're supposed to) and i've tested it outside those times to no joy.

Magnet sensor has it's own indicator light and only registers when I move a magnet past it.

When the sensor does have a magnet move past it - it returns a '1' value in the print window - that in turn is read by MagVal and (should) activate the LED.
That said, I shortcut that process anyway and just had MagVal read the value sent straight from the sensor; but the onboard led still won't light up.

I don't know what a floating pin is sorry (if you mean a loose connection - i've triple checked all connections - and again just now incase)

I put spaces in the code to identify the different sections and the sensors they activate - one giant block of code isn't intuitive for me to read yet. This format makes it much easier for me to read soz.

UPDATE:
So I have made a very slight change...

#define SensorLED 13  //on-board LED assigned to pin 13
#define VibSensorINPUT 3  //vibration sensor on pin 3 (must be ~3 pin)
#include <Wire.h>  //magnet sensor library

unsigned char state = 0;  //vibration sensor check state

int RGBred = 5;  //connect red LED pin to 5 pin
int RGBgreen = 6;  //connect green LED pin to 6 pin
int RGBblue = 7;  //connect blue LED pin to 7 pin

int TouchSensorINPUT = 4;  //connect Touch sensor on pin 4

int MagSensorINPUT = 2; //set pin 2 for magnet sensor signal wire
int MagVal;  //magnet 'on' value

void setup() {
  
pinMode(SensorLED, OUTPUT);  //sensor detect triggers onboard LED
pinMode(VibSensorINPUT, INPUT);  
attachInterrupt(1, blink, FALLING); //trigger the blink function when vibration sensor moves

pinMode(RGBred, OUTPUT);  //assign pin 5 to red RGB LED
pinMode(RGBgreen, OUTPUT);  //assign pin 6 to red RGB LED
pinMode(RGBblue, OUTPUT);  //assign pin 7 to red RGB LED

pinMode(TouchSensorINPUT, INPUT);  //set touch sensor pin to input mode

Serial.begin(9600);  //magnet sensor detect code
Serial.println("magnet:");
pinMode(MagSensorINPUT,INPUT); //asigns MagSensorINPUT to pin 2

}

void loop() {

if (state != 0) {  //vibration sensor detect code
  state = 0;
  digitalWrite(SensorLED, HIGH);
  digitalWrite(RGBred, HIGH); //RGB LED test code
  delay(1000); //red eyes stay on for 1 second
  } 
else {
  digitalWrite(SensorLED, LOW);  //vibration sensor detect code
  digitalWrite(RGBred, LOW); }

    if (digitalRead (TouchSensorINPUT) == HIGH)  //touch sensor detect code
    { 
    digitalWrite(SensorLED, HIGH);
    digitalWrite(RGBgreen, HIGH);
    delay(1000);
    }
    
    else {
    digitalWrite(SensorLED, LOW);  //touch sensor detect code
    digitalWrite(RGBgreen, LOW);
    }

  MagVal = digitalRead(MagSensorINPUT);  //magnet sensor detect code
  Serial.println(MagVal);
  if (MagVal==1) {digitalWrite(SensorLED, HIGH);
  digitalWrite(RGBblue, HIGH);  //NEW LINE OF CODE
  delay(1000); //NEW LINE OF CODE
  } 
  
  else { digitalWrite(SensorLED, LOW);  //magnet sensor detect code
  digitalWrite(RGBblue, LOW);  //NEW LINE OF CODE
  }
  
}
  

void blink() {  //interrupt function for vibration sensor 
    state++;
}

So now, the RGB should go BLUE for 1 second after the magnet sensor is activated as well. Which...it does...AND the on-board LED works as it should.
So that's great, it works. But why??? I've changed nothing else and just added an external led to activate; why does that also allow the onboard led to suddenly work where it didn't before?

This comment is completely screwy:

pinMode(RGBred, OUTPUT);  //assign pin 5 to red RGB LED

Pin 1 is reserved for serial

attachInterrupt(1, blink, FALLING); //trigger the blink function when vibration sensor moves

variables used by both an interrupt and the main program must be declared "volatile"

unsigned char state = 0;  //vibration sensor check state

Pin 1 is reserved for serial

So it's a good thing that the first argument to attachInterrupt() isn't a pin number.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile: