How to update a text file rather than create a new one to over-write it?
in
Programming Questions
•
2 years ago
I have processing running to an arduino currently.
The idea is, you press a button on the arduino, and processing writes the number 1, 2 or 3 to a file depending on what button is pressed.
This file is then read by javascript elsewhere that displays a picture related to that number.
What I'm currently stuck with is getting the file to be written consistently. It seems to work, but every now and again I get flash of no images while running my javascript. I've worked out its because when the file is written to each time, its being created with nothing in it for a split second. (ie, its an empty blank file)
Is there any way I can just edit the file, rather than re-creating a new one with the same name each time?
Thanks
Sam
Here's my code if it's of any help:
The idea is, you press a button on the arduino, and processing writes the number 1, 2 or 3 to a file depending on what button is pressed.
This file is then read by javascript elsewhere that displays a picture related to that number.
What I'm currently stuck with is getting the file to be written consistently. It seems to work, but every now and again I get flash of no images while running my javascript. I've worked out its because when the file is written to each time, its being created with nothing in it for a split second. (ie, its an empty blank file)
Is there any way I can just edit the file, rather than re-creating a new one with the same name each time?
Thanks
Sam
Here's my code if it's of any help:
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
PrintWriter output;
// constants won't change. They're used here to
// set pin numbers:
int greenbuttonPin = 4; // the number of the pushbutton pin
int greenledPin = 11; // the number of the LED pin
int amberbuttonPin = 3; // the number of the pushbutton pin
int amberledPin = 12; // the number of the LED pin
int redbuttonPin = 2; // the number of the pushbutton pin
int redledPin = 13; // the number of the LED pin
int LEDVal = 0;
boolean red = false;
boolean amber = false;
boolean green = true;
long time; //store time here for timing from time to time
// variables will change:
int redbuttonState = 0; // variable for reading the pushbutton status
int amberbuttonState = 0; // variable for reading the pushbutton status
int greenbuttonState = 0; // variable for reading the pushbutton status
void setup() {
time = millis(); //set time = now
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(redledPin, Arduino.OUTPUT);
// initialize the pushbutton pin as an input:
arduino.pinMode(redbuttonPin, Arduino.INPUT);
// initialize the LED pin as an output:
arduino.pinMode(amberledPin, Arduino.OUTPUT);
// initialize the pushbutton pin as an input:
arduino.pinMode(amberbuttonPin, Arduino.INPUT);
// initialize the LED pin as an output:
arduino.pinMode(greenledPin, Arduino.OUTPUT);
// initialize the pushbutton pin as an input:
arduino.pinMode(greenbuttonPin, Arduino.INPUT);
}
void draw(){
// read the state of the pushbutton value:
redbuttonState = arduino.digitalRead(redbuttonPin);
// read the state of the pushbutton value:
amberbuttonState = arduino.digitalRead(amberbuttonPin);
// read the state of the pushbutton value:
greenbuttonState = arduino.digitalRead(greenbuttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (redbuttonState == arduino.HIGH) {
red = true;
amber = false;
green = false;
}
else if (amberbuttonState == arduino.HIGH) {
amber = true;
red = false;
green = false;
}
else if (greenbuttonState == arduino.HIGH) {
green = true;
amber = false;
red = false;
}
if (green == true) {
arduino.digitalWrite(amberledPin, arduino.LOW);
arduino.digitalWrite(redledPin, arduino.LOW);
print("green true");
try {
output = createWriter("file.txt");
output.println("1"); //Write "1" to the file
output.flush(); // Write the remaining data
output.close(); // Finish the file
}
catch(Exception e) {
println("error: ");
}
while (millis() >= time + 5000)
{
// turn LED on/off:
LEDVal = 1 - LEDVal;
if(LEDVal == 1) {
arduino.digitalWrite(greenledPin, arduino.HIGH);
}
else if(LEDVal == 0) {
arduino.digitalWrite(greenledPin, arduino.LOW);
}
time = millis(); //record current time
// arduino.digitalWrite(greenledPin, arduino.LOW);
}
}
else if (amber == true) {
arduino.digitalWrite(greenledPin, arduino.LOW);
arduino.digitalWrite(redledPin, arduino.LOW);
print("amber true");
try {
output = createWriter("file.txt");
output.println("2"); //Write "2" to the file
output.flush(); // Write the remaining data
output.close(); // Finish the file
}
catch(Exception e) {
println("error: ");
}
while (millis() >= time + 5000)
{
// turn LED on/off:
LEDVal = 1 - LEDVal;
if(LEDVal == 1) {
arduino.digitalWrite(amberledPin, arduino.HIGH);
}
else if(LEDVal == 0) {
arduino.digitalWrite(amberledPin, arduino.LOW);
}
time = millis(); //record current time
}
}
if (red == true) {
arduino.digitalWrite(greenledPin, arduino.LOW);
arduino.digitalWrite(amberledPin, arduino.LOW);
print("red true");
try {
output = createWriter("file.txt");
output.println("3"); //Write "3" to the file
output.flush(); // Write the remaining data
output.close(); // Finish the file
}
catch(Exception e) {
println("error: ");
}
while (millis() >= time + 5000)
{
// turn LED on/off:
LEDVal = 1 - LEDVal;
if(LEDVal == 1) {
arduino.digitalWrite(redledPin, arduino.HIGH);
}
else if(LEDVal == 0) {
arduino.digitalWrite(redledPin, arduino.LOW);
}
time = millis(); //record current time
}
}
}
2