Help with code from sensors and motor output

Sorry to ask but im struggling with code,

the sketch looks at inputs from 2 sensors and when a sensor is high it turns a led on and a output to a motor for 10 seconds then turns off and the cycle starts again

it works ok until i connect the motor which is ran from a npn transistor then the 10 seconds on rate is anything from 1-10 seconds

int ledPin = 12;                //  vibration output
int led = 10;                   //  pir output 
int vibPin = 7;                 // choose the input pin (for Vib sensor)
int pirPin = 6;                // choose the input pin (for PIR sensor)
int motor_pin=8;

int val = 0;                    // variable for reading the pin status

void setup() {

  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);     // declare sensor as input 
  pinMode(vibPin, INPUT);     // declare sensor as input 
  pinMode(motor_pin,OUTPUT);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(led, OUTPUT);      // declare LED as output
}

void loop(){
  int val;
  
   val=digitalRead(vibPin);  // read input value
  if(val==1)
   digitalWrite(motor_pin,HIGH);
    digitalWrite(ledPin, HIGH);  // turn LED ON
      delay(10000);
        digitalWrite(motor_pin,LOW);



  val = digitalRead(vibPin);  // read input value
    if (val == HIGH) {            // check if the input is HIGH
     digitalWrite(led, HIGH);  // turn LED ON
       digitalWrite(motor_pin,HIGH);
           delay(10000);
          digitalWrite(motor_pin,LOW);


   
  } else {


    digitalWrite(ledPin, LOW); // turn LED OFF
      digitalWrite(led, LOW); // turn LED OFF
        digitalWrite(motor_pin,LOW);
          delay(300);    
  }
  }

Your topic has been moved from the Covid 19 Emergency Response category to the Programming category of the forum

What possessed you to post it where you did ?

The indentation looks really bad… are you missing {}?

make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac

looks like there are 2 attempts to do the same thing in loop -- read the vibPin and control the motor and led pins. one should be deleted

another issue is if the vibpin is still active, the motor and LED just get turned on again. should the code wait for the vibPin to become deactive before attempting to set the motor and led on again

another issue is buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

Hi, @ktnch

What model Arduino are you using?

Can you please post a copy of your circuit, 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.

What are you using as a power supply?

Can you please post a link to specs/data of your motors?

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

So sorry about the post location kind of new to the forum

So a daft mistake was not adding a 220 ohm resistor from the pin 9 out put to the transistor base that turns the motor in and off, soon as I added one it works ok.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.