Robot face coding issue

I have written this code to have a simple head move when someone enters the room. The concept is PIR senses movement in the room, so a simple head with 4 servos (1-Neck horizontal, 2-neck vertical, 3-eye horizontal, and 4- eyelid )
But unfortunately it is not responding. code compiles and loads, but nothing happens.
here is the code, any advice please ?

 
/*simple ROBOT initial sketch
 created by quazimodo
 */

#include <Servo.h> 
 
Servo serv1;  // create servo object to control a servo neck L-R
Servo serv2;  // neck up-down
Servo serv3;  // eyeball
Servo serv4;  // eyelid 
int PirPin = 2;  // Arduino pin 2 tied to PIR.
boolean state = false, state1 = false, state2 = false, state3 = false, state4 = false;

unsigned int uS, randTime, angle1 = 20, angle2 = 30, angle3 = 30, angle4 = 30;
unsigned long current=0, start1=0, start2 = 0, start3 = 0, start4 = 0;
 
void setup() 
{
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  serv1.attach(5);
  serv2.attach(6);
  serv3.attach(9);
  serv4.attach(10);
  pinMode(PirPin, INPUT);
}

void loop() 
{
  

  
  if(PirPin == HIGH )
  {
    
    state  = true;
    
    while(state)
    {
        current = millis();
        
        
        if(current - start1 > 200)
        {
          start1 = current;
          if(angle1 < 150 && state1 == false)
          {
            angle1++;
            if(angle1 == 150)  state1 = true;
            serv1.write(angle1);
          }
          else if(angle1 > 30 && state1 == true)
          {
            angle1--;
            if(angle1 == 30)
            {
              state1 = false;
              state = false;
            }
            serv1.write(angle1);
          }
        }
        
        if(current - start2 > 100)
        {
          start2 = current;
          if(angle2 < 90 && state2 == false)
          {
            angle2++;
            if(angle2 == 90)  state2 = true;
            serv2.write(angle2);
          }
          else if(angle2 > 30 && state2 == true)
          {
            angle2--;
            if(angle2 == 30)         state2 = false;
    
            serv2.write(angle2);
          }
        }
        
        if(current - start3 > 50)
        {
          start3 = current;
          if(angle3 < 60 && state3 == false)
          {
            angle3++;
            if(angle3 == 60)  state3 = true;
            serv3.write(angle3);
          }
          else if(angle3 > 30 && state3 == true)
          {
            angle3--;
            if(angle3 == 30)         state3 = false;
    
            serv3.write(angle3);
          }
        }
        
        if(current - start4 > 1000*randTime)
        {
          start4 = current;
          randTime = random(1,4);
          if(state4 == false)
          {
            angle4 = 60;
            state4 = true;
            serv4.write(angle4);
          }
          else
          {
            angle4 = 30;
            state4 = false;
    
            serv4.write(angle4);
          }
        }
        
        Serial.println(angle1);
        Serial.println(angle2);
        Serial.println(angle3);
        Serial.println(angle4);
        Serial.println();
       
    }
    
    serv4.write(30);
    
  
  }
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

   Serial.println();
  
}

Hi,
How are you powering the 4 servos?

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

Have you written code to basically control each servo, like sweep each in turn?

What controller are you using?
Are you getting anything on the IDE monitor?

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

This condition is never satisfied because the variable PirPin contains the value 2.
What I'm sure you intended is to READ the value of pin 2 and compare with HIGH, not the value 2:

if(digitalRead(PirPin) == HIGH )

Hi Tom. here is a schematic and photo.
I tested the PIR externally putting a transistor and LED to read output, found it perfectly working...this is why I need help to find out if code is faulty.


Yep, but it seems you haven't read my advice (post #3). Are you using digitalRead() now?

Hi docdoc, my laptop was not available recently. Just now i tested your solution. many thanks it does solve the issue.
now servos respond when motion detected. keeps running for 2 or 3 minutes. Then halt waiting for next motion detection.

please educate me why the previous line didn t work.

I already told you that. The "PirPin" contains the pin number and not the pin value. The pin to be read is the number 2 (i.e. "D2"), and you read that pin value/state by using the "digitalReaad()" function. So "PirPin" is always 2, while "digitalRead(PirPin)" gives you the current pin status, LOW or HIGH depending on the input (a TTL value, either 0 V or 5 V).

1 Like

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