Programming Problem with serialEvent();

Hi,

I am a new user of Arduino UNO.
I have run the blinking examples and now I am trying to create programs of my own.
Right now I have created the following sketch:

int val = 0;
int led1 = 2;
int led2 = 8;
int led3 = 12;
int led4 = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  /*
  char i = Serial.read();
  if (i=='a') {
    digitalWrite(led,HIGH);
  }
  */
  delay(100);
}

void serialEvent() { 
  while (Serial.available()) { 
    val = Serial.parseInt(); 
    if(val == 1) { 
      delay(1000);
      digitalWrite(led1, HIGH); 
      digitalWrite(led2, HIGH); 
      digitalWrite(led3, HIGH); 
      digitalWrite(led4, HIGH); 
    } 
    else if(val == 0) {
      delay(1000); 
      digitalWrite(led1, LOW); 
      digitalWrite(led2, LOW); 
      digitalWrite(led3, LOW); 
      digitalWrite(led4, LOW); 
    } 
  }
  Serial.println("Succesfully received.");   
}

Even though the sketch works for led 13, it does not work for the rest of the leds.
I have already checked the connections on the breadboard.

Any ideas what am I missing?

I call the program from Java:

import jssc.SerialPort;
import jssc.SerialPortException;

public class TestCom {
  public static void main(String[] args) {
        try {
         SerialPort com = null;
 String arduinoComPortNo = "COM7";
 com = new SerialPort(arduinoComPortNo);
 com.openPort();
 com.setParams(9600,8,1,0);
 com.writeString("1");
 //com.writeString("0");
 com.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}

Don't use serialEvent.

How to process incoming serial data without blocking

com.openPort();
            com.setParams(9600,8,1,0);
            com.writeString("1");
            //com.writeString("0");
            com.closePort();

Every time you open the serial port, the Arduino resets. You are not accounting for the time it takes to reset before you send anything.

Use the Serial Monitor app, instead, and you will see different behavior in your program, assuming the LEDs are wired properly.

Don't use serialEvent.

There is nothing inherently wrong with using serialEvent(). Useless, for the most part, but not wrong.

The blocking code IS a problem, but not the root problem.

You may find the examples in Serial Input Basics useful. They are simple, reliable and non-blocking.

...R

Thanks a lot for the input !!!!

I have an extra question @PaulS:

Every time you open the serial port, the Arduino resets.

When Arduino resets, what does it happens on each pin ?

Thanks again !!

Each pin becomes an input. Plus then it runs your code in setup, and then loop again.

Thank you !!!
Hopefully this will be my last question on this thread.
I have a Geovision Net I/O v3.1 card (Manual) - Page 39 connected on a switch button:

  • Cable 1 from switch is connected on Pin 2 of the card
  • Cable 2 (GND) from switch is connected on GND of the card
  • Cable 1 and Cable 2 are connected togehter

When you press the button, then the Geovision Net I/O card instructs the Geovision system to capture an image (jpg) from the cameras and save it on the local disk.

I want to replace the button with an arduino and control it from a web server.
I am using Arduino UNO v3 & Ethernet shield v2:

The connections performed are:

  • Arduino pin 8 is connected on Pin 2 of the card
  • Arduino GND connected on GND of the card
  • Cable 1 and Cable 2 are connected togehter

The code that I wrote was:

#include <SPI.h>
#include <Ethernet2.h>

int photoPin = 8;

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x01, 0xED
};
IPAddress ip(10, 15, 120, 47);
IPAddress gateway(10, 15, 120, 1);
IPAddress subnet(255, 255, 255, 0);

EthernetServer server(80);  

String readString;
String actionReply; 

void setup() {
  pinMode(photoPin, OUTPUT); 
  digitalWrite(photoPin, LOW); 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  
  // For debugging in case of error
  Serial.begin(9600);
  Serial.println("TESTING ARDUINO TAKE PHOTO SYSTEM"); 
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        if (c == '\n') {
          Serial.println("THE REQUEST FROM THE CLIENT IS:");
          Serial.println(readString); 
          // READ THE DATA
          if (readString.indexOf("/takePhoto") > 0) {
            digitalWrite(photoPin, HIGH); 
            delay(5000);
           digitalWrite(photoPin, LOW); 
            // SET RESPONSE
            actionReply = "TRUE";
          }
          else {
            actionReply = "FALSE";
          }          

          // debug response
          Serial.println(actionReply);
          
          // RESPONSE TO THE CLIENT
          client.println("HTTP/1.1 200 OK"); 
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>ANAMET</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>ANAMET</H1>");
          client.println("<hr/>");
          client.println("
");
          client.println("<p>");
          client.println(actionReply);
          client.println("</p>");
          client.println("</BODY>");
          client.println("</HTML>");
          delay(10);
          client.stop();
          // RESET PARAMETERS
          actionReply = "";
          readString = ""; 
        }
      }
    }
  }
}

The problem that I am facing is somehow connected with my previous posts.
When I power up arduino using USB cable (or upload code via the IDE - i.e. it resets), then it takes a photo.

When I run the program, then it does not take photo.

I am trying to reproduce the actions on the pin when it resets.

Thank you again for your precious support.

Well, I think that I understood my problem...

The Geovision card is outputing +5V and if I inverse the pinMode to INPUT then it starts taking photos.

I suppose that I have to alter the code as follows:

#include <SPI.h>
#include <Ethernet2.h>

int photoPin = 8;

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x01, 0xED
};
IPAddress ip(10, 15, 120, 47);
IPAddress gateway(10, 15, 120, 1);
IPAddress subnet(255, 255, 255, 0);

EthernetServer server(80);  

String readString;
String actionReply; 

void setup() {
  pinMode(photoPin, OUTPUT); 
  digitalWrite(photoPin, LOW); 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  
  // For debugging in case of error
  Serial.begin(9600);
  Serial.println("TESTING ARDUINO TAKE PHOTO SYSTEM"); 
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        if (c == '\n') {
          Serial.println("THE REQUEST FROM THE CLIENT IS:");
          Serial.println(readString); 
          // READ THE DATA
          if (readString.indexOf("/takePhoto") > 0) {
            pinMode(photoPin, OUTPUT); 
            delay(100);
            pinMode(photoPin, INPUT); 
            // SET RESPONSE
            actionReply = "TRUE";
          }
          else {
            actionReply = "FALSE";
          }          

          // debug response
          Serial.println(actionReply);
          
          // RESPONSE TO THE CLIENT
          client.println("HTTP/1.1 200 OK"); 
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>ANAMET</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>ANAMET</H1>");
          client.println("<hr/>");
          client.println("
");
          client.println("<p>");
          client.println(actionReply);
          client.println("</p>");
          client.println("</BODY>");
          client.println("</HTML>");
          delay(10);
          client.stop();
          // RESET PARAMETERS
          actionReply = "";
          readString = ""; 
        }
      }
    }
  }
}

Any thoughts ?

Hi,

I just tested the solution and it works !!!

Thanks @Nick Gammon for his help !!