Serial Control over Processing

Hi all.

I have the following code in Arduino that when on initialisation it starts by blinking a led, and only when a command (letter) is sent over the Serial Monitor, the led stops blinking. The stop phase lasts for a little less than 2 minutes, and then starts blinking again, and so on.

The code works fine when I use the Serial Monitor. However, when I do the same in Processing (code below), the led receives the 'a' letter and stops blinking, but never starts again.

Can someone explain why??

#include <Wire.h>
#include "RTClib.h"

RTC_Millis RTC;

float startMin;
float setRandMin;
float count = 0;
int newCount = 0;

int led1 = 13;

char input;
boolean starter = true;

void setup () {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);

  // following line sets the RTC to the date & time this sketch was compiled
  RTC.begin(DateTime(__DATE__, __TIME__));
  DateTime now = RTC.now();
  startMin = now.minute();

  setRandMin = 1;
}

void loop () {
  if (Serial.available()) {
    input = Serial.read();
  }

  DateTime now = RTC.now();
  if (startMin != now.minute()) {
    count = count + 1;
  }

  newCount = count / 60.;

  if (newCount >= setRandMin || starter) {
    float led1Bool = random (0, 2);
    if (led1Bool > 0.7) {
      digitalWrite (led1, HIGH);
    }
    else {
      digitalWrite(led1, LOW);
    }
    delay(200);
  }
  else {
    delay(1000);
  }

  if (input == 'A' || input == 'a') {
    starter = false;
    startMin = now.minute();
    setRandMin = 1;
    count = 0;
    digitalWrite(led1, LOW);
  }
}

/////// Processing Code //////////
import processing.serial.*;
Serial myPort;

void setup() {
println(Serial.list());
myPort = new Serial (this, Serial.list()[0], 9600);
}

void draw() {
}

void mouseReleased() {
myPort.write(65);
}
///////////////////////////////////

  startMin = now.minute();

Why are you storing an integral amount in a float?

There is nothing in your code that makes ANY sense with floats.

Not sure if the float thing had anything to do with the issue, nevertheless, I have changed this inconsistency.

Still, when sending a Serial command via Processing, the led stops (initially correct), but fails to start blinking again after the specified amount of time.

Here is the example again.

#include <Wire.h>
#include "RTClib.h"

RTC_Millis RTC;

int startMin;
int count = 0;

int led1 = 13;

char input;
boolean starter = true;

void setup () {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);

  // following line sets the RTC to the date & time this sketch was compiled
  RTC.begin(DateTime(__DATE__, __TIME__));
  DateTime now = RTC.now();
  startMin = now.minute();
}

void loop () {
  if (Serial.available()) {
    input = Serial.read();
  }

  DateTime now = RTC.now();

  if (startMin != now.minute()) {
    count = count + 1;
  }
  
  if (count >= 60 || starter) {
    float led1Bool = random (0, 2);
    if (led1Bool > 0.7) {
      digitalWrite (led1, HIGH);
    }
    else {
      digitalWrite(led1, LOW);
    }
    delay(200);
  }
  else {
    delay(1000);
  }

  if (input == 'A' || input == 'a') {
    starter = false;
    startMin = now.minute();
    count = 0;
    digitalWrite(led1, LOW);
  }
}

So, I changed this:

if (Serial.available()) {
input = Serial.read();
}

To this:

while(Serial.available() > 0) {
char input = Serial.read();
}

And it works. Probably better to have a while loop checking...

  while(Serial.available() > 0) {
      char input = Serial.read();
  }

I fail to see why you bother reading the serial port, when all you do it is throw the data away.