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