[SOLVED] My code freeze, please help new to Arduino

Hello everyone,

could you please have a look at my very first code, it is supposed to control a door.
I have started with some feedback switches etc, at the end I've simplified it as much as I could.
Arduino still freeze after first movement and I don't get any values via Serial monitor.
I have tried to run it with the digital outputs to Relay board disconnected, Serial monitor/code
seemed to work fine, but when digital outputs are connected it's unpredictable.
I use the pin 5V and GND on Arduino to supply the Relay board, as suggested by manual.

I use these parts:

Arduino Uno

4 channel relay specific for Arduino: Arduino Your Home & Environment: Working with the SainSmart 5v Relay Board

12VDC car battery - voltage is 12.2VDC. I have tried separate Power supply set to 10VDC also to rule out overvoltage.

Light sensing diode to tell Arduino when to close/open door: http://www.banggood.com/4Pin-Photodiode-Sensor-Module-Detection-Module-For-Arduino-p-969265.html?currency=GBP&createTmp=1&utm_source=google&utm_medium=shopping&utm_content=jude&utm_campaign=Electronic-xie-uk&gclid=CNatip3yz8sCFfMV0wodQ3cArA

My code:

int TCC_I1 = 0;
int TCC_I2 = 0;
int TCC_I3 = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  digitalWrite(5, LOW);
  int TCC_I1 = 0;
  int TCC_I2 = 0;
  int TCC_I3 = 0;
  delay(10000);
  digitalWrite(5, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);  // 1Hz Polling sequence speed set here
  int AI0 = analogRead(A0);
  float voltage = AI0 * (5.0 / 1023.0);  // This is voltage from light sensing diode. Darker=More voltage 

  //***********************************************//
  //**Section for closing the door in the evening**//
  //***********************************************//


  if ((voltage > 2.00) && (TCC_I3 == 1)) // It is dark and door is still open ('cause TCC_I3 = 1)
  {
    TCC_I1 = (TCC_I1 + 1);
  }

  if (TCC_I1 == 10) 
  {
    digitalWrite(5, LOW);
    delay(10000);
    digitalWrite(5, HIGH);
    delay(1000);
    TCC_I1 = 0;
    TCC_I3 = 0;
    delay(1000);
  }

  //***********************************************//
  //**Section for opening the door in the morning**//
  //***********************************************//

  if ((voltage < 1.99) && (TCC_I3 == 0))   // It is light and door is still closed ('cause TCC_I3 = 0)
  {
    TCC_I2 = (TCC_I2 + 1);
  }
  if (TCC_I2 == 10)  
  {
    digitalWrite(3, LOW);
    delay(500);
    digitalWrite(4, LOW);
    delay(10000);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);

    TCC_I2 = 0;
    TCC_I3 = 1;
    delay(1000);
  }

  Serial.println(voltage);
  Serial.println(TCC_I1);
  Serial.println(TCC_I2);
  Serial.println(TCC_I3);


}

You'll probably find my programming odd I'm a person using Visual Basic in a very specific automation software, hence I try simple ifs clauses.

Any suggestions are really welcome, I've already spent 2 days and cannot get it to reliably work :(

Kind regards
David

SOLUTION: Use 5VDC USB port, not 12VDC jack. Bypassing the voltage regulator allows control of more relays

you do not close your first IF() statement till much later in the sketch.

Please upload a schematic of your circuit showing everything exactly the way you connected it.

And please post you code in code tags using </> button

I can already see that you have some errors in the brackets in you code.

dorvakta:
Please upload a schematic of your circuit showing everything exactly the way you connected it.

And please post you code in code tags using </> button

I can already see that you have some errors in the brackets in you code.

Hello I don't have schematic I've built it from top of my head, I probably need to draw them for you here (and myself too).

What do you mean by </> button?

Read point 7 in this link http://forum.arduino.cc/index.php/topic,148850.0.html

read the sticky at the top of every forum.

then edit your first post (bottom right at the end of the post)

delete the code on your first post.

open your IDE , and auto format you code.
then copy
paste on your first post with code tags.,

comment out everything between your reading the analog and the last bit where you post it to your serial terminal screen.
what values to you get ? should be above 2 in daylight ?
this should update once per second.

void loop() {

 delay(1000);  set 1Hz polling sequence
 int AI0 = analogRead(A0);
 float voltage = AI0 * (5.0 / 1023.0);

/*     // the slash dot starts to comment out all of your code

all the bits would be here

   delay(1000);
 }

*/     // this is the end of the comment section.

 Serial.println(voltage);
 Serial.println(TCC_I1);
 Serial.println(TCC_I2);
 Serial.println(TCC_I3);

}

dave-in-nj:
comment out everything between your reading the analog and the last bit where you post it to your serial terminal screen.
what values to you get ? should be above 2 in daylight ?
this should update once per second.

void loop() {

delay(1000);  set 1Hz polling sequence
int AI0 = analogRead(A0);
float voltage = AI0 * (5.0 / 1023.0);

/*    // the slash dot starts to comment out all of your code

all the bits would be here

delay(1000);
}

*/    // this is the end of the comment section.

Serial.println(voltage);
Serial.println(TCC_I1);
Serial.println(TCC_I2);
Serial.println(TCC_I3);

}

Hello Dave,
the code works fine and the "TCC" counters do count.
As I mentioned in the first post it seems to work fine when I unplug (disconnect) the wires
which drive the relays. I think I need to draw wiring diagramm.
But generally from the code you can see how the parts are connected.

How are you powering the Arduino? only through the USB port or with an external power supply?

Try to change the relays with LEDs and see if it's working.

prodeng:
Hello Dave,
the code works fine and the "TCC" counters do count.
As I mentioned in the first post it seems to work fine when I unplug (disconnect) the wires
which drive the relays. I think I need to draw wiring diagramm.
But generally from the code you can see how the parts are connected.

if you find that the device freezes when connected, then a complete check of that connection is in order.
the relay boards have 3 separate things.
the outputs are isolated.. no possible problem there.
the relay power and signal lines are connected.
often you connect grounds and feed 5v through a resistor to the opto's.
most popular problem is not connecting grounds.

You can power one relay from Arduino's 5volt line with a 10volt supply on the DC jack.
You might be able to power two for a short while. The onboard 5volt regulator will get hot.
You can't power more than two.

A cigarette lighter supply with USB socket might be able to power the Arduino and four relays.
That bypasses the onboard 5volt regulator.
Leo..

Wawa:
You can power one relay from Arduino's 5volt line with a 10volt supply on the DC jack.
You might be able to power two for a short while. The onboard 5volt regulator will get hot.
You can't power more than two.

A cigarette lighter supply with USB socket might be able to power the Arduino and four relays.
That bypasses the onboard 5volt regulator.
Leo..

Hi Leo,
I was under the impression that 500mA is allowed, and that this 4 relay module should be fine.
I measured current flowing when 2 relays are ON, it was 260mA drawn from 5V pin of Arduino.
Also - Arduino manual suggests not to bypass the 5V regulator - they say damage could be caused.
I could do this for a try though - I have a 5VDC power supply..

I have also uploaded a wiring diagramm of my installation. The connections themselves couldn't be more simple...

Kind regards
David