0
votes

I am creating a program that gets the serial input, converts it into an int and then sets a pin to HIGH for however long the input says, such as if I print "9000" to the Arduino serial port it will set pin 13 to HIGH for 9000 milliseconds. However, it sets the pin to HIGH for the initial "handshake" at the start of the connection. Is there any way of eliminating this, or countering it?

Here is my current Arduino code:

int Relay = 13;
//The pin that the relay is attached to
int time;
//Creates temp variable

void setup() {
    //Initialize the Relay pin as an output:
    pinMode(Relay, OUTPUT);
    //Initialize the serial communication:
    Serial.begin(9600);
}

void loop() {
    while(true) {
        //Check if data has been sent from the computer:
        if (Serial.available()) {
                    //Assign serial value to temp
            time = Serial.parseInt();
            //Output value to relay

                    delay(4000);
                digitalWrite(Relay, HIGH);
                delay(time);
            digitalWrite(Relay, LOW);
        }
    }
}

If there is any way could you let me know how I could do it?

Thanks :)

Edit 1:

int Relay = 13;
//The pin that the relay is attached to
int time;
byte byte_1;
byte byte_2;
//Creates temp variable

void setup() {
  pinMode(Relay, OUTPUT);
  //Initialize the serial communication:
  Serial.begin(9600);
  digitalWrite(Relay, LOW);
  delay(250);
}

void loop() {
  //Check if data has been sent from the computer:
  if (Serial.available() == 2) {
      byte_1 = Serial.read();
      delay(100);
      byte_2 = Serial.read();
  }                   

  if(byte_1 == 16) {
      digitalWrite(Relay, HIGH);
      delay(byte_2);
      digitalWrite(Relay, LOW); 
  }

  byte_1 = 0;
  byte_2 = 0;
}

Edit 3:

package net.arduino;

import java.io.OutputStream;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

public class Main {
    public static void main(String[] args) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM3");

        if(portIdentifier.isCurrentlyOwned()) {
            System.out.println("ERROR!!! -- Port already in use!");
        }else{
            CommPort commPort = portIdentifier.open("XBell", 0);

            if(commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                OutputStream out = serialPort.getOutputStream();
                byte[] buffer = new byte[2];
                buffer[0] = (byte) 16;
                buffer[1] = (byte) 1000;

                out.write(buffer);
                out.flush();
                Thread.sleep(10000);
                out.close();
            }else{
                System.out.println("ERROR!!! -- This is not normal");
            }
        }
    }
}
1
You should be able to just digitalWrite(Relay, LOW); in your setup(). It may even work to do that before setting it as an output. You can go after the reset issue either with host operating system configuration (state of the control lines when idle), by disabling the serial-commanded reset, by changing the bootloader to drive the signal low immediately, or even by disabling the reset line in fuses (though that will make changing your program hard and be a pain to undo). If erroneous setting of the signal has negative real-world consequences, you may need a safer design.Chris Stratton

1 Answers

0
votes

You are also not checking the input for validity(time = Serial.parseInt()) so any noise on the line will act as a valid input value. I wrap my calls to the Arduino with a command protocol so that I can be sure the Arduino got the sent value and not noise. i.e. first send 16 for GetReadyToReceive, then the value. That way there is less chance of a false input.

See my code at http://playground.arduino.cc/Csharp/SerialCommsCSharp for an example