Hi,
I have this sketch in were I change (int maxnum) the number of cycles needed, in the code. I want to be able to input the number of cycles through the serial port. The second set of code shows my attempt at this. When I run the code the loop only runs the line of code once. I don't know what I am doing wrong.
int valve1 = 11;
int valve2 = 12;
int maxnum = 10; // CHANGE VALUE TO TOTAL AMOUNT OF CYCLES NEEDED !!!
int cycles = 0;
int count = 0;
void setup() {
pinMode(valve1, OUTPUT);
pinMode(valve2, OUTPUT);
Serial.begin(9600);
Serial.println("Cycles");
}
void loop()
{
if (cycles < maxnum)
digitalWrite(valve1, HIGH);
delay(500);
digitalWrite(valve1, LOW);
delay(1000);
if (cycles < maxnum)
digitalWrite(valve2, HIGH);
delay(500);
digitalWrite(valve2, LOW);
delay(1000);
count = count+1;
cycles = cycles+1;
if (count < maxnum)
Serial.println(count);
}
int valve1 = 11;
int valve2 = 12;
int cycles = 0;
int result;
char rx_byte = 0;
String rx_str = "";
boolean not_number = false;
void setup() {
Serial.begin(9600);
Serial.println("ENTER NUMBER OF CYCLES");
pinMode(valve1, OUTPUT);
pinMode(valve2, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); // get the character
if ((rx_byte >= '0') && (rx_byte <= '9')) {
rx_str += rx_byte;
}
else if (rx_byte == '\n') {
// end of string
if (not_number) {
Serial.println("Not a number");
}
{
if (cycles < result)
digitalWrite(valve1, HIGH);
delay(500);
digitalWrite(valve1, LOW);
delay(1000);
digitalWrite(valve2, HIGH);
delay(500);
digitalWrite(valve2, LOW);
delay(1000);
cycles = cycles+1;
}
not_number = false; // reset flag
rx_str = ""; // clear the string for reuse
}
else {
// non-number character received
not_number = true; // flag a non-number
}
} // end: if (Serial.available() > 0)
}