Notifications
Clear all

Open two svingdoors with two servos in random angles using two IR sensors along a railroad track

6 Posts
2 Users
0 Likes
558 Views
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

PS I am learning Arduino for my model railway in H0e scale.

My goal: Opening and closing of a double hinged door. The right door opens first then the other. Each will open min 90 deg. and max 180 degre but diferent and unequal deg. each time. I use Arduino uno R3, 2 SG90 servos and 2 IR MH sensors or equal. I build my sketch fom simple to more advanced. The following sketch works fine. Servo 1 will only close or do nothing. Servo 2 (nearest the doors),will only open or do nothing. But I hope to achieve more. See below the sketch

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA=5;
int pinIRB=6;
int pinServo1=9;
int pinServo2=10;
int val=0;

void setup(){

}

void loop(){
val = digitalRead(pinIRA);

if (val ==0){
Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
Serv1.write(0);
Serv2.write(180);
delay(500);
Serv1.detach();
Serv2.detach();
}
else {;}


val = digitalRead(pinIRB);

if (val ==0){
Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
Serv1.write(170);
Serv2.write(50);
delay(500);
Serv1.detach();
Serv2.detach();
}
else {;}

}

I have next introduced the random function. When the doors open by signal from IR2, random shall not deliver new values until doors have been closed by IR1 This is where I struggle with the code. Also I get an compilation error: "value required as left operand of assignement"  Any suggestions to solve this will be greatly appreciated Here is the code:

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA=5;
int pinIRB=6;
int pinServo1=9;
int pinServo2=10;
int val=0;

// a variable to hold a random number
int randomNumber1;
int randomNumber2;

void setup(){

Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
}

void loop(){

val = digitalRead(pinIRA);

if (val==0){
Serv1.write(0);
Serv2.write(180);
delay(200);
}
else{ ; }
delay(200);

// assign a random number to our variable
randomNumber1 = random(91,181);
randomNumber2 = random(0,91);

switch (val) {
case 1:
//do something when val equals 0
val = digitalRead(pinIRB);
(val==0);{
Serv1.write(randomNumber1);
Serv2.write(randomNumber2);
break;
}
delay(200);

case 2:
//do something when val equals 1
val = digitalRead(pinIRB);
(val==1);
&&
val = digitalRead(pinIRA);
(val==1);{
Serv1.write( ; );
Serv2.write( ; );
break;
}
delay(200);

}

error noted on line after &&: val = digitalRead(pinIRA);


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@keknor

The lvalue error is the line:

&&val = digitalRead(pinIRA);
and it means that &&val is not something to which a value can be assigned (i.e. it's not a valid variable). You may have meant:
 
val = digitalRead(pinIRA);
 
But this really doesn't make ant sense. because you have 
 
//do something when val equals 1
val = digitalRead(pinIRB);
(val == 1);
&&val = digitalRead(pinIRA);
(val == 1);
So what you're doing is reading the value of pin pinIRB into val, then comparing that value with 1 and then setting the value of val to what's on pinIRB and then comparing the value of val back to 1. Kinda pointless. You should probably be doing something like 
if (val==1) { /* do something clever here */ }
 
Another problem is the lines
Serv1.write(;);
Serv2.write(;);
 
The ; is not a valid value for a servo position. They often run from 0-180, ';' is not within that valid range.
 
Try fixing these errors and see what happens, if it's still not working, come back 🙂
 
 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@keknor

Is this closer to what you were trying to do ?

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA = 5;
int pinIRB = 6;
int pinServo1 = 9;
int pinServo2 = 10;
int val = 0;

void setup() {
  Serv1.attach(pinServo1);
  Serv2.attach(pinServo2);
}

void loop() {

  if (digitalRead(pinIRA) == 0) {
    Serv1.write(0);
    Serv2.write(180);
  }
  delay(200);

  if (digitalRead(pinIRB) == 0) {
    Serv1.write(random(91, 181));
    Serv2.write(random(0, 91));
  }
  delay(200);

  if (digitalRead(pinIRA) == 1) {
    Serv1.write(90);
    Serv2.write(90);
  }
  delay(200);
}

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

Posted by: @keknor

PS I am learning Arduino for my model railway in H0e scale.

My goal: Opening and closing of a double hinged door. The right door opens first then the other. Each will open min 90 deg. and max 180 degre but diferent and unequal deg. each time. I use Arduino uno R3, 2 SG90 servos and 2 IR MH sensors or equal. I build my sketch fom simple to more advanced. The following sketch works fine. Servo 1 will only close or do nothing. Servo 2 (nearest the doors),will only open or do nothing. But I hope to achieve more. See below the sketch

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA=5;
int pinIRB=6;
int pinServo1=9;
int pinServo2=10;
int val=0;

void setup(){

}

void loop(){
val = digitalRead(pinIRA);

if (val ==0){
Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
Serv1.write(0);
Serv2.write(180);
delay(500);
Serv1.detach();
Serv2.detach();
}
else {;}


val = digitalRead(pinIRB);

if (val ==0){
Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
Serv1.write(170);
Serv2.write(50);
delay(500);
Serv1.detach();
Serv2.detach();
}
else {;}

}

I have next introduced the random function. When the doors open by signal from IR2, random shall not deliver new values until doors have been closed by IR1 This is where I struggle with the code. Also I get an compilation error: "value required as left operand of assignement"  Any suggestions to solve this will be greatly appreciated Here is the code:

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA=5;
int pinIRB=6;
int pinServo1=9;
int pinServo2=10;
int val=0;

// a variable to hold a random number
int randomNumber1;
int randomNumber2;

void setup(){

Serv1.attach(pinServo1);
Serv2.attach(pinServo2);
}

void loop(){

val = digitalRead(pinIRA);

if (val==0){
Serv1.write(0);
Serv2.write(180);
delay(200);
}
else{ ; }
delay(200);

// assign a random number to our variable
randomNumber1 = random(91,181);
randomNumber2 = random(0,91);

switch (val) {
case 1:
//do something when val equals 0
val = digitalRead(pinIRB);
(val==0);{
Serv1.write(randomNumber1);
Serv2.write(randomNumber2);
break;
}
delay(200);

case 2:
//do something when val equals 1
val = digitalRead(pinIRB);
(val==1);
&&
val = digitalRead(pinIRA);
(val==1);{
Serv1.write( ; );
Serv2.write( ; );
break;
}
delay(200);

}

error noted on line after &&: val = digitalRead(pinIRA);

 


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  
1 (2)

Thank you Will.

Your sketch will not do what I try to achieve. I will try to explain better:

Drawing show a train to the right with two wagons aproaching the shed to the left with the two doors. The doors are normally closed. IRA have only one function; To close the doors. Therefor nothing happens when the train pass IRA. When the train pass IRB, the doors open. A random nr. to each servo opens the doors each to different angles >90deg. The train may shunt back and forth and therfore activate IRB several times. But I do not want any action on the servos, the doors must stand still. When the train leave, it will activate IRA and the doors will close. Next time the train come back the loop continue.

About (;) This is a solution I found on youtube. IR will only give signal to servo when activated by the train. When the train have passed there will be no signal to open the doors again . This works fine. I have tested it. IRA can only give signal to the servos to close the doors and IRB can only give signal to open.

My challenge with the code is to only get one new random nr.  to each servo. When the train have passed the IRA and the doors close, then when the next train arrives and activates IRB, a new random number will be given to the servomotors so that the doors open to new angles>90deg.  etc, etc,. 

So I think the code must check the status of IRA. If the IRA is not activated, there will be no new random nr.

How can I accomplish this?


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@keknor 

OK, lemme be sure I have this right ...

IRA causes the doors to close (only)

IRB causes the doors to open some random amount but only does so when the doors are closed. That is, if the doors closed its signs a random opening amount for each door. If the doors are not closed, then IRB does nothing.

Does that sound right to you ? If so, try this sketch.

#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIRA = 5;
int pinIRB = 6;
int pinServo1 = 9;
int pinServo2 = 10;
bool isClosed = true;             // Is door closed

void setup() {
  Serv1.attach(pinServo1);
  Serv2.attach(pinServo2);
  randomSeed(analogRead(0));      // Initialize random number generator
  Serv1.write(0);                 // Close doors to start
  Serv2.write(180);
}

void loop() {

  //
  //    If IRA is triggered and the doors are open, then close them
  //
  if (digitalRead(pinIRA) && !isClosed) {
    Serv1.write(0);
    Serv2.write(180);
    isClosed = true;              // Doors are closed
  }
  //
  //    IF IRB is triggered and the doors are closed, then open them some
  //
  if (digitalRead(pinIRB) && isClosed) {
    Serv1.write(random(91, 181));
    Serv2.write(random(0, 91));
    isClosed = false;             // Doors aren't closed
   }
}

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote