BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Arduino mega Servomotor controlled by ir sensor

arduino
#1

Hello! I have a project in witch i want to open doors using servomotors and ir sensors, i have 4 of each. I try to create functions for each door and use milies() do create a delay for the door to stay open for a bit . but only one door ,function “usa_camera” is working , and i don’t why the others don’t.
Here is the code:

#include<Servo.h>
Servo usca; //servomotor camera
Servo usba; //servomotor baie
Servo usbuc; //servomotor bucatarie
Servo usfa; //servomotor fata

int ir1=22; // senzor camera
int ir2=23; // senzor baie
int ir3=24; // senzor bucatarie
int ir4=25; // senzor fata

const int interval=5000;
unsigned long alta=0; //Memoreaza timpul

void setup() {
// put your setup code here, to run once:
usca.attach(12);
usba.attach(11);
usbuc.attach(5);
usfa.attach(9);

usca.write(170);
usba.write(180);
usbuc.write(0);
usfa.write(180);
pinMode(ir1,INPUT);
pinMode(ir2,INPUT);
pinMode(ir3,INPUT);
pinMode(ir4,INPUT);

Serial.begin(9600);
}

void loop()
{
unsigned long timp= millis();
usa_camera(timp);
usa_baie(timp);
usa_bucatarie(timp);
usa_fata(timp);
}

//////////Usa Camera
void usa_camera(int timp)
{

if(digitalRead(ir1)==0)
{
usca.write(60);
}

else if(digitalRead(ir1)==1)
{if(timp-alta>=interval)
{
alta=timp;
usca.write(170);
}
}
}

//////////Usa Baie
void usa_baie( int timp)
{

if(digitalRead(ir2)==0)
{
usba.write(60);
}

else if(digitalRead(ir2))
{if(timp-alta>=interval)
{
alta=timp;
usba.write(180);
}
}
}

////// //Usa Bucatarie
void usa_bucatarie(int timp)
{

if(digitalRead(ir3)==1)
{
usbuc.write(120);
}

else if(digitalRead(ir3)==0)
{ if(timp-alta>=interval)
{
alta=timp;
usbuc.write(0);
}
}
}
///////////Usa Fata
void usa_fata(int timp)
{

if(digitalRead(ir4)==1)
{
usfa.write(180);
}
else if(digitalRead(ir4)==0)
{
if(timp-alta>=interval)
{
alta=timp;
usfa.write(70);

}
}
}

0 Likes

#2

This isn’t related to Blynk in any way and you haven’t read the rules regarding posting.

  • I suggest you try the Arduino.cc forum
  • Also it would be useful to know what connections you’re using and what hardware etc

You also need to read up on creating functions and passing parameters, to point you in slightly the right direction, your declaring timp as an unsigned long and passing it as an int.

0 Likes

#3

Please edit your post to add triple backticks at the beginning and end of your code.
Triple backticks look like this:
```

Pete.

0 Likes