HI, i'm having trouble understanding what's wrong in this program:
const int ledPin = 8; // the pin that the LED is attached to
int x=0;
int brightness=-1;
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// check if data has been sent from the computer:
if (Serial.available())
{
brightness = Serial.read();
if (brightness == 49)
{
digitalWrite(ledPin, HIGH); // set the LED on
Serial.print("true");
}
}
Serial.println(brightness);
}
shouldnt pin 8 stay hight once the value 49 is received?
Why does it go LOW when it starts a new cycle in the looP?
when u say "u keep opening the serial port " u mean in the arduino code right?
Thanks so much ...i spent last 2 hours on this, i thought i was doing something wrong in the java part of the code...
I changed to this and now theres just a little flickering when data sending stops, but is there a better way thtat prevent the flicker?
void loop() {
if (x=1)
{
digitalWrite(ledPin, HIGH);
}
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
if (brightness == 49)
{
digitalWrite(ledPin, HIGH); // set the LED on
Serial.print("true");
// wait for a second
x=1;
}
when u say "u keep opening the serial port " u mean in the arduino code right?
Wrong.
The Arduino doesn't have an "open" operation on the serial port, so how could I mean that?
I cant see where do I keep opening the serial port? could u point it to me?
And I didn't say
"u keep opening the serial port "
simply because I wouldn't, because I can spell "you".
well i'm glad u can, it's not that dificult.lol
RoyK:
haven't checked your code for function but pretty sure
if (x=1) should be if (x == 1) ...
thanks, thats for sure....lol that explains the flickering..and on the toher hand it wouldn't solve my problem either way.
edited-----
I'm pretty sure the only time i open the serial port is here:
.....
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
[/quote]
does the arduino get reseted when the port is closed also?
EDIT 2................................................................................................
thanks in advance!
I can now confirm that what resets the arduino is the port.close call on the java side. Is this normal??
It's unthinkable to keep the port always opened... any suggestions??
Yes, its normal: It's expected behavior, in fact, as the DTR pin is used for the auto-reset functionality in the Arduino IDE. If you disable the DTR pin entirely, you can close the port without issues.
As for whether or not you should keep the port open: I don't think you quite understand how ports/sockets are normally treated. It's like writing to a file: as long as you intend to modify the file at least one more time before the program closes, the file should be kept open, to ensure that no OTHER program edits the file (locking). You do NOT open/close on every operation - this is inefficient and tends to lead to unexpected things happening. For a serial connection, especially, IO is expected to be continued pretty often until the program is closed, and the port should only be closed upon the ending of the program.
I guess it will have to do it... i was only thinking that the comunication will occur just a few times in very well defined time( user presses some buttons), so i would like to let another application use the port in the mean time.
I'll go with the port always open but if i may ...one more question how could i disable the DTR pin (if its easy, if not i'll google it)?
Thanks
EDIT-------------->> OK i just went back to Netbeans(my IDE) and just found out how to do it(by luck, because i thought you were referring to disble it in arduino), at least i think i did as i dont have the time to test it.
I'll go with the port always open but if i may ...one more question how could i disable the DTR pin (if its easy, if not i'll google it)?
If you want to prevent the arduino reset, either put a 100 ohm resistor between the arduino +5v and the arduino reset pin, or put a large value capacitor between the arduino reset pin and the arduino ground.
I'll go with the port always open but if i may ...one more question how could i disable the DTR pin (if its easy, if not i'll google it)?
If you want to prevent the arduino reset, either put a 100 ohm resistor between the arduino +5v and the arduino reset pin, or put a large value capacitor between the arduino reset pin and the arduino ground.
thanks a lot for your help.
PaulS:
if (brightness == 49)
6 months from now, are you going to remember what 49 means?
if (brightness == '1')
performs exactly the same test, but makes it easy to remember what to send to the Arduino to make it perform the intended task.