I have a program running with three buttons that represent 3 warning stages. OK, Minor Problem and Major Problem.
If the OK button is pressed, nothing happens and a 1 is written to a text file.
If the Minor button is pressed, a vibration motor pulses for a moment every 5 seconds or so. A 2 is written to the file.
If the Minor button is pressed, the vibration motor pulses like a heartbeat. A 3 is written to the file.
The difficulty I'm having is that after a while, the vibrations seem to stop working even though the rest of the program appears to work fine. I end up having to stop processing and start it again.
Can anyone see what I might have done to cause this problem?
I have a feeling it's something to do with millis() running out, or getting confused. I'm a noob so I'm still learning.
Here's my code in full...
import processing.serial.*; import cc.arduino.*;
Arduino arduino; PrintWriter output;
int okbuttonPin = 4; // the ok button int minorbuttonPin = 3; // the minor button int majorbuttonPin = 2; // the major button int vibrationPin = 13; // the vibration motor
int vibVal = 0; //vibration timing counter
boolean major = false; boolean minor = false; boolean ok = true;
long time; long waitUntil = 0; long startLoopTime; float startTime;
int majorbuttonState = 0; // variable for reading the major button status int minorbuttonState = 0; // variable for reading the minor button status int okbuttonState = 0; // variable for reading the ok button status
int check = 0; //variable to check the number written to the file
void setup() {
time = millis(); //set time = now
arduino = new Arduino(this, Arduino.list()[0], 57600);
//initialize the vibration motor as an output arduino.pinMode(vibrationPin, Arduino.OUTPUT);
//initialize all the buttons as inputs arduino.pinMode(majorbuttonPin, Arduino.INPUT); arduino.pinMode(minorbuttonPin, Arduino.INPUT); arduino.pinMode(okbuttonPin, Arduino.INPUT);
}
void draw(){
//if 2 seconds have passed, enable the buttons (by changing them back to inputs) if (millis() > startTime + 2000) { fill(255); arduino.pinMode(majorbuttonPin, Arduino.INPUT); arduino.pinMode(minorbuttonPin, Arduino.INPUT); arduino.pinMode(okbuttonPin, Arduino.INPUT); } rect(20, 20, 20, 20);
// read the state of the buttons majorbuttonState = arduino.digitalRead(majorbuttonPin); minorbuttonState = arduino.digitalRead(minorbuttonPin); okbuttonState = arduino.digitalRead(okbuttonPin);
// check if the major button is pressed. if (majorbuttonState == arduino.HIGH) { major = true; minor = false; ok = false; }
// check if the minor button is pressed. if (minorbuttonState == arduino.HIGH) { minor = true; major = false; ok = false; }
// check if the ok button is pressed. if (okbuttonState == arduino.HIGH) { ok = true; minor = false; major = false; }
//if the ok button has been pressed if (ok == true) {
//do not vibrate arduino.digitalWrite(vibrationPin, arduino.LOW);
//write 1 to the file if it hasn't done so already if (check != 1) { print(" ok ");
output = createWriter("file.txt"); output.println("1"); //Write "1" to the file output.flush(); // Write the remaining data output.close(); // Finish the file
//change check variable to 2 so the file is only written to once check = 1;
// temporarily disable input from buttons by making them outputs startTime = millis(); fill(0); arduino.pinMode(majorbuttonPin, Arduino.OUTPUT); arduino.pinMode(minorbuttonPin, Arduino.OUTPUT); arduino.pinMode(okbuttonPin, Arduino.OUTPUT); }
//write 2 to the file if it hasnt done so already if (check != 2){ print(" minor fault ");
output = createWriter("file.txt"); output.println("2"); //Write "2" to the file output.flush(); // Write the remaining data output.close(); // Finish the file
//change check variable to 2 so the file is only written to once check = 2;
// temporarily disable input from buttons by making them outputs startTime = millis(); fill(0); arduino.pinMode(majorbuttonPin, Arduino.OUTPUT); arduino.pinMode(minorbuttonPin, Arduino.OUTPUT); arduino.pinMode(okbuttonPin, Arduino.OUTPUT); }
//write 3 to the file if it hasnt done so already if (check != 3){
print("major fault"); output = createWriter("file.txt"); output.println("3"); //Write "2" to the file output.flush(); // Write the remaining data output.close(); // Finish the file
//change check variable to 3 so the file is only written to once check = 3;
// temporarily disable input from buttons by making them outputs startTime = millis(); fill(0); arduino.pinMode(majorbuttonPin, Arduino.OUTPUT); arduino.pinMode(minorbuttonPin, Arduino.OUTPUT); arduino.pinMode(okbuttonPin, Arduino.OUTPUT); }
I have a set up running three buttons at the moment. I'm having trouble with my program crashing when somebody presses all three buttons in quick succession.
To stop this from happening, when one button is pressed, I want to disable input from all the buttons for 1 second.
I'd rather not use a delay, so ideally it would be using millis.
I'm really struggling on figuring out a couple of lines of code to do this though, millis really confuses me and I'm not sure how you disable a button in processing.
I have a section of code that loops every 5 seconds and switches an LED to be on or off for those 5 seconds.
What I'm aiming to do, is have the LED switch on for 1 second, and switch off for the remainding 4 seconds, while still having the loop run a second time and keep the LED off for 5 seconds.
I tried using a delay in the space I've commented below, but the delay stops all input from processing for that amount of time which I don't want.
Can anyone help?
Thanks
if (red == true) { while (millis() >= time + 5000) {
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:
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 }
try { output = createWriter("file.txt"); output.println("3"); //Write "3" to the file output.flush(); // Write the remaining data output.close(); // Finish the file }