Arduino does not work when it is not plugged in by usb

Hi everyone!

so I have an issue with my Arduino.
i am making an automatic chicken coop door. The code seems to 'sometimes' ( without changing anything it sometimes doesn't or does) run when I connect it to my laptop, however. when I eject the usb it stops functioning. It does do a little bit of the first proces, but then stops and ignores every command.
I hope someone can help me. :confused:
I attached the wiring diagram and the code.

Thanks in advance :slight_smile: !

//Chicken coop door

int DoorGoodmorning = 550; // lightvalue when door must go open
int DoorGoodnight = 450; // lightvalue when door must close
int DoorPulses = 35; // amount of pulses needed to close the door ( travel 35cm)

const int DoorMeasurements = 5; // Average of the amount of measurements to decide
const int intervalvalue = 60; // amount of seconds to pause between measurements
const int motortin1Pin = 5;
const int motortin2Pin = 4;
const int magnetPin = A1;
const int ldrPin = A2; // LDR

const int GreenLed = 9;
const int RedLed = 8;

// variables
int intervalcounter = 0;
int doorStatus = 1; // doorStatus, 1=open, 0=closed
int intMeasureMoment = 0; // position in arrey of lightmeasurements
uint16_t light[DoorMeasurements]; // array with the lightmeasurements

//
void setup(void) {
/
/

Serial.begin(57600);
Serial.println("ChickenDoor");

pinMode(motortin1Pin, OUTPUT);
pinMode(motortin2Pin, OUTPUT);

pinMode(GreenLed, OUTPUT); // The Green LED turns on when the door is closed
pinMode(RedLed, OUTPUT); // Red LED turns on when the door is in opening or closing sequence

pinMode(ldrPin, INPUT); //ldr+plus en ldr+port+10k
pinMode(magnetPin, INPUT_PULLUP); //between 0 and the pin

// filling measurement array with default average
for (int counter = 0; counter < DoorMeasurements; counter++) {
light[counter] = (DoorGoodmorning + DoorGoodnight) / 2;
}

// setting variables
doorStatus = 0;
intMeasureMoment = 0;

// at start the door closes a lttle and then re-opens to sync the motor and check if LED's work
TestDoor();

}

//
void TestDoor(void) {
/
/
digitalWrite(motortin1Pin, LOW);
digitalWrite(motortin2Pin, HIGH);

digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, HIGH);

for (int counter = 0; counter < 5; counter++) delay(1000);
OpenDoor();
}

//
void CloseDoor(void) {
/
/

digitalWrite(motortin1Pin, LOW);
digitalWrite(motortin2Pin, HIGH);
for (int counter = 0; counter < DoorPulses; counter++) delay(1000);
// motor uit
digitalWrite(motortin1Pin, LOW);
digitalWrite(motortin2Pin, LOW);

digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}

//
void OpenDoor(void) {
/
/

digitalWrite(RedLed, HIGH);
digitalWrite(GreenLed, LOW);

while (digitalRead(magnetPin) == 0) { //when value = 0, the door is not (fully) open
digitalWrite(motortin1Pin, HIGH);
digitalWrite(motortin2Pin, LOW);
}
// motor off
digitalWrite(motortin1Pin, LOW);
digitalWrite(motortin2Pin, LOW);

digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, LOW);
}

//
void ProcesDoor(void) {
/
/

uint16_t average = 0;

//retrieve lightmeasurements
light[intMeasureMoment] = analogRead(ldrPin); // put measured value in array
intMeasureMoment++; // rise array position
if (intMeasureMoment >= DoorMeasurements) intMeasureMoment = 0; // if array is filled, start at beginning with filling

// calculate average of the array
average = 0;
for (int counter = 0; counter < DoorMeasurements; counter++) {
average += light[counter];
}
average = average / DoorMeasurements;

// decide if door should open or close
if ((average <= DoorGoodnight) && (digitalRead(magnetPin) == 1)) { // door must be open, if it is going to close
CloseDoor();
doorStatus = 1;
}

if (average >= DoorGoodmorning) {
OpenDoor();
doorStatus = 0;
}

}

//
void loop(void) {
/
/

//taskcompletion. must be every minute
intervalcounter = 0;
while (intervalcounter < intervalvalue) {
delay(1000);
intervalcounter++;
Serial.print("LDR Value: ");
Serial.print(analogRead(ldrPin));
Serial.print(" Door Status: ");
Serial.print(doorStatus);
Serial.print(" Magnet switch: ");
Serial.println(digitalRead(magnetPin));
}

ProcesDoor();
}

Documentscans 13.pdf (198 KB)

Vin needs a minimum of about 7V. If you only have 5V it should go to the 5V pin not Vin.

Steve

Hi Steve!

thanks for the tip!
I changed it, however the issue is still there..
any other ideas on what may be causing the issue?

regards,
Friso