Works at first, then super buggy

I’m trying to rig a few IR proximity sensors on a train layout, ie when the train goes by, the sensor fires one of two loops: one fires 2 12v relays I’m using as a flasher for a crossing signal, the second fires a small servo.

It ran fine for a couple of runs, then got super buggy. All that happens when I turn on arduino is the flasher loop keeps running and won’t stop. So I commented out everything but the 12v flasher relay combo and the ir sensor and it still loops the flasher code without stopping.

I don’t think I have a setup issue because it all worked initially.

I’d post my layout, but don’t see a way to post pics in this forum. Code is below.
Thanx for your help!!


#include <Servo.h>
 
int servoPin = 8;
int sweepy = 0;
int iBlink;
int flasherOut;  
int flasherPin = 6;  
 
Servo servo;  


void setup() {

  
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  servo.attach(servoPin);


}

void loop() {
  Serial.print("IRSensorip ");

//fire crossing flasher
flasherOut = digitalRead(flasherPin);
if (flasherOut == LOW){
    iBlink = 0;
    while(iBlink<5){
      digitalWrite(13, HIGH);
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
      digitalWrite(12, HIGH);
      delay(200);
      digitalWrite(12, LOW); 
      delay(200); 
      iBlink++;

    }    
   }  

 //fire servo 
 if(digitalRead(4)==LOW){
    sweepy = 0;
    while(sweepy <5){
       servo.write(20);      
       delay(10000);        
       servo.write(100);     
       delay(5000);
       servo.write(0);
       sweepy++;
  }  
 }
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

The Arduino is not a power supply. Sounds like there may be a power problem. How are the relay coils and servo powered?

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

it is so easy to post pictures that it is not obvious:

Simply copy your picture into the clipboard and then paste the clipboard by pressing Ctrl-V it into the posting.
This forum-software is pretty modern.

best regards Stefan

do the inputs need to be configured as INPUT_PULLUP with internal pullup resistors?

it's not uncommon for the output os such detectors to simply be a transistor that pulls low so that they can be used with a variety of circuity operating at different voltages

consider

// IR/Flasher code

#undef MyHW
#ifdef MyHW
byte PinIrs  [] = { A1, A2 };
#else
byte PinIrs  [] = { 6, 4 };
#endif

byte PinLeds [] = { 13, 12 };
#define Nled  sizeof(PinLeds)

unsigned long msecLst [Nled];

unsigned long MsecFlash = 200;
unsigned long msec;

enum { Off = HIGH, On = LOW };


// -----------------------------------------------------------------------------
void
checkIr (void)
{
    for (unsigned n = 0; n < Nled;  n++)  {
        if (HIGH == digitalRead (PinIrs [n]))  {
            digitalWrite (PinLeds [n], Off);
        }
        else if (msec - msecLst [n] >= MsecFlash)  {
            msecLst [n] = msec;
            digitalWrite (PinLeds [n], ! digitalRead (PinLeds [n])); 
        }
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();

    checkIr ();
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < Nled;  n++)  {
        pinMode (PinIrs [n], INPUT_PULLUP);

        pinMode (PinLeds [n], OUTPUT);
        digitalWrite (PinLeds [n], Off);
    }
}
1 Like

I think that’s my problem - servo is running straight off of the board. Working on a schematic so I can post it here. Thanx for the tip.

I’ll have to stare at that for a bit :grinning:
Thanx so much, I’ll give it a try.

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