Go Down

Topic: sensor sporadically failing (Read 802 times) previous topic - next topic

treebykooba

Hello,

I have these two motion sensors that were given to me awhile ago. They take 10-30V (i'm giving them 12 regulated) and ouput 200mA which I have in the analog pins. I have them on voltage dividers so they don't output too much to the arduino pin...so they're outputting around 2V, giving me a steady serial reading of around 240.

I'm using the sensors for a kinetic sculpture...an object is raised by a pulley, when the top sensor sees the object, it gets lowered, when the bottom sensor sees the object it gets raised.

my problem is, is that sometimes the bottom sensor doesn't see the object and it continues to get lowered. this happens very sporadically...the motion could on for 2 hours and then all of a sudden miss a reading. if i wave my hand in front of it, it starts up again and everything continues. I need this thing to be able to run perfectly, though...for 8 hours a day for around a month.

any ideas why this sensor might not get a reading every once and awhile? any ideas for how I could tweak it so that it works better?

the code to activate the "upward motion" relays is just: if ((sensorBottom > 50)&&(switchBottom == false)) { ....

thanks so much for any help!

robtillaart

PLease post your whole code as that makes it far easier to debug .
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

treebykooba

here is the code
i put the sensor readings throughout the loop and it's been working straight for 2 and a half hours now. Is that a fix or are there other things i could do better?
thanks so much for any feedback

Code: [Select]
int sensorBottom, sensorTop;
boolean switchTop = false;
boolean switchBottom = false;
int switchey = 1;
int unSpoolCounter = 1;
int upDown = 1;
int LED1 = 11; //all on PWM pins
int LED2 = 10;
int LED3 = 9;
int LED4 = 5;


void setup() {

  //CW is up and CC is down

  pinMode (13, OUTPUT); //DPDT relay
  pinMode(12, OUTPUT); //SPST relay

  pinMode(LED1,OUTPUT); //4 12V LEDs through tip120s
  pinMode(LED2,OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  pinMode (7, OUTPUT); //DPDT relay for motorBottom
  pinMode (6, OUTPUT); //spst relay for motorBottom

  Serial.begin(9600); //debug

//start by picking up
  //when everything is hi,
  //motor goes in clockwise
  //if red is in DPDT relay
  //and black is in 5v SPST relay
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
 

}

void loop() {
  sensorBottom = analogRead(0);
  sensorTop = analogRead(1);


  //get stuck in infinite if loops//

  //if top sensor is activated wait for 4 sec and then let down
  if ((sensorTop > 40)&&(switchTop == false)){
    //digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    delay(4000);
    //flip switch
    switchBottom=false;
    switchTop=true;

  }
  if (switchTop == true) {
    //lower
    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
  }
 
  sensorBottom = analogRead(0);


  if ((sensorBottom > 10)&&(switchBottom == false)) {
    delay(100); //delay a bit because bottomSensor is too high
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);

    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    delay(200);
    digitalWrite(LED4, LOW);
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    //2) turn on all lights and spin motor
    delay(200);
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);

    if (switchey == 1) {
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
    } else {
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      switchey = 0;         
    }

    switchey++;
    delay(6000);

    //3)turn everything off and start over again
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(6, LOW);

    switchBottom=true;
    switchTop=false;

    unSpoolCounter++;
    upDown++;
    if (unSpoolCounter > 10) {
      //if it has done so many rotations
      //pick it up a bit
      digitalWrite(13, HIGH);
      digitalWrite(12, HIGH);
      delay(500);
      //and let it unravel for ten seconds while lights pulsate
      digitalWrite(12,LOW);
      pulsate();     
      unSpoolCounter = 0;
    }
    if (upDown > 15) {
      upAnddown ();
      upAnddown ();
      upDown = 0;
    }
  }

  if (switchBottom == true) {   
    //pick UP
    digitalWrite(13, HIGH);
    digitalWrite(12, HIGH);
  }
 
  sensorBottom = analogRead(0);

}

/////////mysterious functions/////////////
void pulsate () {

  for (int i = 0; i < 7; i++) {     
    // fade in from min to max in increments of 5 points:
    for(int fadeValue = 10 ; fadeValue <= 255; fadeValue +=5) {
      // sets the value (range from 0 to 255):
      analogWrite(LED1, fadeValue);
      analogWrite(LED2, fadeValue);         
      analogWrite(LED3, fadeValue);         
      analogWrite(LED4, fadeValue);             
      // wait for 30 milliseconds to see the dimming effect   
      delay(30);                           
    }

    // fade out from max to min in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue >= 10; fadeValue -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(LED1, fadeValue); 
      analogWrite(LED2, fadeValue);         
      analogWrite(LED3, fadeValue);         
      analogWrite(LED4, fadeValue);             
      // wait for 30 milliseconds to see the dimming effect   
      delay(30);                           
    }

  }

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
}


void upAnddown () {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);

    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
delay(2500);
digitalWrite(13, LOW);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
delay(1500);
digitalWrite(12,LOW);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
delay(1000);

}

robtillaart

A delay of 4000 (or 6000) means that you have no sensorreadings for 4(6) seconds. Anything can happen !!

Advice: completely rewrite the code so that it does not use delay(). check - http://arduino.cc/en/Tutorial/BlinkWithoutDelay - for the basis technique

Think of your code as a state machine going from one state to the other depending on input, flags and timers.
-> isolate the measurements, the processing and the actions
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

MarkT

Also you say the sensors output current yet you read them with a voltage divider?  I think you mean a load resistor?
[ I won't respond to messages, use the forum please ]

Go Up