Need help with new project coding!

Need some assistance with coding. I currently have a motion sensor music box that when activated cranks an old music machine. The code and the Electonics work great! here is the code that works

const int pirPin = 3;
const int motorPin = 2;
int switchState = 0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(motorPin, OUTPUT);
  pinMode(pirPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  switchState = digitalRead(pirPin);
 
  if (switchState == HIGH) {
    digitalWrite(motorPin, HIGH);
  }
  else {
    digitalWrite(motorPin, LOW);
  }
}

The issue I am having is that I wanted to add a secondary switch for touch / electromagnetic but when I upload the new code for it, the motor will not spin with motion and just continuously spins when the other sensor is in slot 4. I am just checking to see if my coding is wrong and to see what I can do to have 2 sensors hooked up to trigger the motor. Here is the new code I am trying to get to work.

#include <CapacitiveSensor.h> // Import capaitivesenosr library
CapacitiveSensor cs_4 = CapacitiveSensor(5,4); // 1M resistor between pins 2 & 4, pin 4 is sensor pin, add a wire and 
const int pirPin = 3;
const int motorPin = 2;
int switchState = 0;
int in = 5; 
int out = 4;  
// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(motorPin, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(4, INPUT);
}

// the loop routine runs over and over again forever:
void loop()
 { 

  switchState = digitalRead(pirPin);
  switchState = digitalRead(4);
 
  if (switchState == HIGH) {
    digitalWrite(motorPin, HIGH);
  }
  else {
    digitalWrite(motorPin, LOW);
  }

}

Anything helps!

  • First things first, show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
 switchState = digitalRead(pirPin);
 switchState = digitalRead(4);

You're using the same variable for two different inputs. I didn't look any further.

Just guessing here but, maybe you want something like:

switchState = (digitalRead(pirPin) or digitalRead(4));

Using this, switchState would be (logically) true if either input returns a HIGH value.

The red circle is the only thing new that I have added

Thank you! I will give this a try!

  • You have labeled the component connected to pin 4 tmp, what is this, capacitive sensor ?
    Does this work by itself ?

  • What is the MOSFET part number ?

  • What kind of battery are you using, PP3 ?

  • You have labeled the component connected to pin 4 tmp, what is this, capacitive sensor ? It is not temp but the touch sensor I have has three prongs and I couldn't find that kind of sensor on tinkercad. Below is a picture of the sensor.

Does this work by itself ? I use the sensor on other projects but it needs a host.

  • What is the MOSFET part number ? L7805CV

  • What kind of battery are you using, PP3 ? Yes 2 of them. Originally had only one but it would die after 2 hours I added one to the board where the plug us and then the other to the breadboard where the second one is positioned. Now it last 6.5 hours

  • The above is a Hall Effect Sensor, (magnetic sensor).

  • L7805CV, this is a 5 volt regulator.

  • Use a 9v battery holder, 6 AA batteries (6 * 1.5v = 9v).

1 Like

L7805CV is not a MOSFET, it is a 5V linear regulator, why are you using it?

Can you post some images of your project?
So we can see your component layout.

PLEASE.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Just wanted to update. This worked!!! Thank you so much

:+1:

1 Like

Can you please post your working code.

What about the LM7805?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

It is a custom of this forum that solutions be posted so as to be of a help to others with similar problems or requirements.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia: