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...
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);
}
}
//----------------------------------------------------------------------
//if the minor button has been pressed
if (minor == true) {
while (millis() >= time + 500)
{
// vibrate once then wait a while to do it again
vibVal = 1 + vibVal;
if(vibVal == 1) {
arduino.digitalWrite(vibrationPin, arduino.HIGH);
}
else if(vibVal == 2) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 3) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 4) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 5) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 6) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 7) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 8) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 9) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 10) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 11) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 12) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
vibVal = 0;
}
time = millis(); //record current time
}
//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);
}
}
//----------------------------------------------------------------------
//if the major button has been pressed
if (major == true) {
//pulse the vibration motor like a heartbeat
while (millis() >= time + 500)
{
vibVal = 1 + vibVal;
if(vibVal == 1) {
arduino.digitalWrite(vibrationPin, arduino.HIGH);
}
else if(vibVal == 2) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 3) {
arduino.digitalWrite(vibrationPin, arduino.HIGH);
}
else if(vibVal == 4) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 5) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
}
else if(vibVal == 6) {
arduino.digitalWrite(vibrationPin, arduino.LOW);
vibVal = 0;
}
time = millis(); //record current time
}
//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);
}
}
}
1