Arduino Stops Serial Printing

I am doing some testing trying to record how long a faucet runs while some one is in front of a sensor and soap is dispensed.

I had this code working fine for about a week but I tried moving it to a different location and setting it up. Now, when testing it (commenting out the main algorithm) to calibrate what the values should be in the main algorithm it stops printing when soap is dispensed and restarts, see attached. I haven't been able to find a solution.

purell1.ino (2.51 KB)

It would be helpful for you to post the code you're having problems with.

Sorry, I tried to attach it. Should I just copy/paste?
See below:

float faucet, soap, soap1 = 0, occupant, occupant1;
double t;
int i, j, k, pumps, trials = 0;

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

Serial.println("--------------------");
Serial.println("Turbine Code v5");
Serial.println("Program records faucet and dispenser activity 30sec after soap is dispensed");
Serial.println("--------------------");
Serial.println("");

delay(2000);

}

void loop() {

soap = (analogRead(0)*5)/1023.0; //measures soap dispenser voltage from pin A0
faucet = (analogRead(1)*5)/1023.0; //measures turbine voltage from pin A1
occupant = (analogRead(2)*5)/1023.0; //measures occupant sensor voltage from pin A2

//-----------for test only---------//
// Serial.print("soap: ");
// Serial.print(soap);
// Serial.print(" faucet: ");
// Serial.print(faucet);
// Serial.print(" occupant: ");
// Serial.println(occupant);
//---------------------------------//

//---------------main algorithm---------------//
if(occupant > 0.45 & occupant1 > 0.45) { //senses that someone is there
soap1 = soap; //updates values of soap1 and occupant1, they aren't needed before
occupant1 = occupant;

while(1) { //endless loop while someone is using the sink
soap = (analogRead(0)*5)/1023.0;
faucet = (analogRead(1)*5)/1023.0;
occupant = (analogRead(2)*5)/1023.0;

if(occupant < 0.40) { //if person leaves, check everything and print if needed

if(pumps == 0) { //won't print if there was no soap usage
t = 0;
pumps = 0;
break;
}

trials++; //increments trial number and prints out all data
Serial.print(trials);
Serial.print(" ");
Serial.print(t/100);
Serial.print(" ");
Serial.println(pumps);

pumps = 0; //sets values back to 0 for next measurement
t = 0;
break;
}

if(faucet > 1.45) { //turbine will output voltage > 1.45V if water is running
t++; //measures time water was on
}

if(soap > .10 && soap1 < .10 ) { //soap dispenser voltage is active low, measures falling edge
pumps++;
}

soap1 = soap; //soap1 and occupant1 hold pervious values to look for changes
occupant1 = occupant;
delay(10);
}
}

occupant1 = occupant;
delay(10);
}

Should I just copy/paste?

No.

it stops printing when soap is dispensed and restarts

Power issue?
Can you post a schematic?

The faucet part is a bit more complicated than is shown here but the faucet is reading fine. Just when the soap dispenser motor activates and outputs a voltage it stops.

Are you really trying to drive motors with an Arduino output. No wonder it resets. Arduino output can, safely, supply 20mA, 40mA absolute maximum. You need an external power source for the motors and a proper motor driver to power the motors. The proper driver depends on the motor rated voltage and stall current and whether the motor runs in only one direction or must be reversible.

No sorry, they both have external power that is working fine.

A schematic should be an accurate representation of a circuit. A schematic should show all components and their values and/or part numbers and power sources. I should be able to reproduce your circuit from just the schematic.

Hi

When you say restarts, are you saying that the arduino reboots?
ie do the lines


Turbine Code v5
Program records faucet and dispenser activity 30sec after soap is dispensed

re-appear on your output?

If so it would indicate to me that there is an electrical fault somewhere, probably an earthing issue.

Yes, it does. I will check all of the connections that go to ground