Author Options:

Arduino stop the loop? Answered

i have a problem with my code, i basically move a motor with the help of 3 relays, when a sensor does not detect the presence of an object part of a call, everything works perfectly, if it was after having called the function CHIAMATA() the arduino loop() stop unexplains, i certainly made a mistake, but i do not understand which.

thanks for your help this is the code....

#include <swRTC.h>
#include <SoftwareSerial.h>
#include <String.h>

swRTC rtc; //create a new istance of the lib
SoftwareSerial mySerial(3, 2);

const int buttonPin = 19;
const int sensorPin = 9;
const int allarmPin =  13;

int buttonState = 0;
int sensorState = 0;
int relay1 = 8;
int relay2 = 7;
int relay3 = 6;
int var = 0;

// initialize the stepper library on pins 8 through 11:

void setup() {
    rtc.stopRTC(); //stop the RTC
    rtc.setTime(12,0,0); //set the time here
    rtc.setDate(4,6,2012); //set the date here
    rtc.startRTC(); //start the RTC
  Serial.begin(19200); //choose the serial speed here
  pinMode(allarmPin, OUTPUT);

void loop() {


    Serial.print(rtc.getHours(), DEC);
    Serial.print(rtc.getMinutes(), DEC);
   Serial.print(rtc.getSeconds(), DEC);
    Serial.println(" ");

//stato riserva croccantini
sensorState = digitalRead(sensorPin);

if (sensorState == HIGH) {
    // turn LED on:
    digitalWrite(allarmPin, HIGH);
    Serial.println("Croccantini esauriti");
    Serial.println("Spia allarme ACCESA");
    Serial.println("Tramoggia DISABILITATA");
    Serial.println("Imposto la chiamata");
    while(sensorState == HIGH) {CHIAMATA(); break;}
  } else {
    // turn LED off:
    digitalWrite(allarmPin, LOW);
    Serial.println("Croccantini OK");
if (sensorState == LOW && rtc.getHours()== 12 && rtc.getMinutes()== 00 && rtc.getSeconds()== 5) {startmotor();}
  //start manuale carico/scarico
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
    if (buttonState == 1) {
      Serial.print("Pulsante alimentazione manuale ATTIVO");                          
    // turn on:
  // singlestartmotor();


void startmotor(){
//numero di cicli di carico da eseguire (5)
while(var < 5){
pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);

digitalWrite(relay3, LOW); //ABILITA TUTTO
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
delay(1000); // wait 2 seconds

digitalWrite(relay3, HIGH);//STOP TUTTO
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);

digitalWrite(relay3, LOW);//ABILITA TUTTO
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
delay(1000); // wait 2 seconds

  digitalWrite(relay3, HIGH);//STOP TUTTO
  delay(5000); // wait 2 seconds


void CHIAMATA() {
mySerial.println("ATD329xxxxx135;");//dial the number, xxxx ix the phone number.
Serial.print("Chiamata in corso..."); return loop();}



8 months ago

I cannot get my head around three relays when you only need two.

One (SPST) for stop/run and one (DPDT) forward/reverse..

Are there inverse parallel diodes across the relay coils ?


If you use an H bridge configuration you only need 2 double pole double throw relays and 2 outputs from the controller.

1 on 2 off motor goes one way.

1 off 2 on motor goes the other way

1 on 2 on motor stop

1 off 2 off motor stop.

H brige.jpg

Good idea for using an H-bridge and better use of identical SPDT relays..

darn spdt your right even cheaper

Yes, and easy to find everywhere..

BTW after reading some of OP code that 3d relay could be for speed change.. But we will probably never hear from this newbie..

A simple flow chart (graphic or text) would explain at a glance

It is offensive that stranger Cris expects me to spend my time to study his code until my mind creates the Flow Chart he should have provided !

keep calm, I did not have time to answer, I need to 2 relay for reverse the motor gear (relay 2 and 3), while the relay 1 I need to send in safety mode, the engine and other utilities that are not described in the code.

Anyway, I managed to solve the problem, in essence I sent an infinite loop to the line 1 of the loop, (while (mySerial.available ()! = 0); {} delay (10);) running the white loop at the CHIAMATA() function, the problem has been resolved.


void CHIAMATA() {

Serial.println("Imposto la chiamata");

mySerial.available()!=0; delay(10);

mySerial.println("ATD329xxxxx135;");//dial the number, xxxx ix the phone number.


Serial.print("Chiamata in corso...");



Click the pic

To see whole image.


8 months ago

It would be far more valuable to learn how to debug, troubleshoot, and fix your own code. I don't mean this in an offensive way. It is a valuable skill that you should develop. Go your your code, construct a flow diagram of exactly how it should be implemented. Make sure that each and every line of code does exactly what you think it does without side effects. (C++ can have many gotcha's, such as pointers and references which can trip up even experienced coders.)


To troubleshoot, figure out exactly how your code isn't working. What aspect of it seems broken, exactly? With the arduino environment, it's helpful to just throw in a bunch of Serial.println lines to check the state at critical locations and make sure that what the values for the variables and stuff makes sense.


If your code is 'stopping' somewhere, then you are using a blocking i/o model, which is almost always very poor programming practice, although very occasionally it's unavoidable. You need to use a non blocking i/o.or you have a more serious bug somewhere (like a segmentation fault or something) causing your code to crash.