Pages: [1]   Go Down
Author Topic: arduino + processing, program runs only once  (Read 1106 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am trying to run arduino, from processing GUI interface I created myself.
I have the function, that while hall sensor does not sense magnet (arduino.HIGH), it keeps the led on, and when magnet approaches the hall sensor (hall sensor switches to arduino.LOW), the function turs off the led and exits the function.

here is the code of the function
Code:
void bottomDeadCenter() {                                 
  int hallSensor = arduino.digitalRead(hallPin);                         //read hall value
  while (hallSensor == arduino.HIGH) {
    arduino.digitalWrite(onPin, arduino.HIGH);                         //ifthere is no magnet, turn on the led
    hallSensor = arduino.digitalRead(hallPin);
  }
  arduino.digitalWrite(onPin, arduino.LOW);                           //ifthere is magnet, turn off the led
  return;                                                                                // stop the function
}

the problem I have, is that the function runs only once - when i start processing, compile the code and run it, the function functions perfectly. when I close the program, recompile it, the function does not work anymore. Only when I restart the computer and recompile the code, the function works once again for only one time smiley-sad

does the function or the program must contain some memory flush etc i don't know about?

p.s. my programing level is basic.
Logged

Detroit, MI
Offline Offline
Full Member
***
Karma: 0
Posts: 117
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Provide your full Arduino code, and your full Processing code; I'm having trouble understanding what you aim to accomplish without it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

been away from civilisation for a while, now back to project smiley

I am using the Standard Firmrata, uploaded into arduino, and it's library in the Processing, to control the arduino.

The code in the processing is very simple:
there is a button, which when pressed, executes the function.
But that function works only once. closing and reopening application does not help. only if i retart the computer, it works only once again for one time smiley-sad

the code in the processing is:
Code:

// screen background color
int backgroundColor = color(150);          //background colour

// load the font
PFont font;                                //declare the font variable

// button operation variables
boolean BDC = false;                     
boolean loadCell = false; 
int bdcColor = color(227, 23, 23);
int loadCellColor = color(200);
int startColor = color(200);

// setup Arduino communication
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int hallPin = 9;                          // hall sensor pin (digital read)
int onPin = 7;                             // on / off switch pin (digital write)

void setup() {

  // screen setup
  size(1000, 680);                         // screen size
  smooth();                                // smooth lines
  noStroke();                              // no lines
  background(backgroundColor); 
  fill(245);
  //rect(15, 115, 970, 600);
  stroke(200);                             // chart grey lines
  line(23, 190, 977, 190);                 // chart horizontal line
  line(23, 290, 977, 290);                 // chart horizontal line
  line(23, 490, 977, 490);                 // chart horizontal line
  line(23, 590, 977, 590);                 // chart horizontal line
  stroke(0);                               // chart black lines
  line(width/2, 106, width/2, 672);        // chart center vertical line
  line(7, 390, 993, 390);                  // chart center horizontal line

  //input / output pins setup
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(onPin, Arduino.OUTPUT);
  arduino.pinMode(hallPin, Arduino.INPUT);
}

// bottom dead center function
void bottomDeadCenter() {
  arduino.digitalWrite(onPin, arduino.LOW);
  int hallSensor = arduino.digitalRead(hallPin);                         //read hall value
  while (hallSensor == arduino.HIGH) {
    arduino.digitalWrite(onPin, arduino.HIGH);                           //if there is no magnet, turn on the motor
    hallSensor = arduino.digitalRead(hallPin);
  }
  arduino.digitalWrite(onPin, arduino.LOW);                              //if there is magnet, turn off the motor
  return;                                                                // stop the function
}


void draw() {

  smooth();

  // colors for interactive button backgrounds
  int red = color(227, 23, 23);
  int green = color(65, 190, 55);
  int grey = color(200); 

  //load the font
  font = loadFont("Verdana-18.vlw");        //load the font
  textFont(font);                           //set the current text font

    noStroke();
  fill(190);                                // red buttons shadow
  rect(23, 23, 120, 73);
  rect(163, 23, 120, 73);
  rect(303, 23, 120, 73);

  stroke(170);                              // red buttons
  strokeWeight(1);

  fill(bdcColor);                           // BDC BUTTON
  rect(20, 20, 120, 73);
  fill(255);
  text("BDC", 40, 65);

  fill(loadCellColor);                      // LOAD CELL BUTTON
  rect(160, 20, 120, 73);
  fill(255);
  text("RESET", 180, 55);
  text("Load Cell", 180, 75); 

  fill(startColor);                         // START BUTTON
  rect(300, 20, 120, 73);
  fill(255);
  text("START", 320, 65);

  font = loadFont("Verdana-12.vlw");
  textFont(font);
  fill(46, 42, 42);
  strokeWeight(0);
  text("LoadCellVoltageRef", 450, 45);      // INFO TEXT
  text("maxVelocity", 660, 45);
  text("Stroke", 850, 45);
  text("maxLoad", 450, 85);
  text("minLoad", 620, 85);
  text("OMEGA", 850, 85);

  font = loadFont("Verdana-18.vlw");
  textFont(font);

  //BDC BUTTON OPERATION
  if (BDC == false && loadCell == false) {
    if (mouseX > 20 && mouseX < 140 && mouseY > 20 && mouseY < 93) {
      if (mousePressed == true && mouseButton == LEFT) {                        //check the button if it is pressed
        bottomDeadCenter();
        bdcColor = green;
        loadCellColor = red;
        BDC = true;
      }
    }
  }

  // LOAD CELL OPERATION
  if (BDC == true && loadCell == false) {
    if (mouseX > 160 && mouseX < 280 && mouseY > 20 && mouseY < 93) {
      if (mousePressed == true && mouseButton == LEFT) {                  //check the button if it is pressed
        loadCell = true;
        loadCellColor = green;
        startColor = red;
      }
    }
  }

  // START BUTTON OPERATION
  if (BDC == true && loadCell == true) {
    if (mouseX > 300 && mouseX < 420 && mouseY > 20 && mouseY < 93) {
      if (mousePressed == true && mouseButton == LEFT) {                //check the button if it is pressed
        startColor = green;
      }
    }
  }
}




there are other butons, but i haven't creaed functions for them yet.

also the program does not seem to respond quickly to the mouse click. It needs the longer press of the mouse button to be "clicked" smiley-sad

any thoughts are appretiated.

I am using windows 7, 64 bit
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  //load the font
  font = loadFont("Verdana-18.vlw");        //load the font
  textFont(font);                           //set the current text font
Why are you doing this in draw()? The draw() function, like loop(), is executed over and over. There is no reason to reload the font every time. Create multiple global font variables, and load the fonts in setup().

Quote
But that function works only once.
Because the test for mouse location and mouse pressed occurs inside an if test that tests whether BDC is false. After calling the function, you set BDC to true, so the if(BDC == false) part will never be true again.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

thanx PaulS for the comments.

I am loading the font in the draw function, because I use two size fonts, and if I set in in the setup, it uses only one font.

Or should I do in setup 
Code:
void setup() {
  font_1= loadFont("font_one.vlW");        //load the font 1
  font_2= loadFont("font_two.vlW");        //load the font 2
 }
void loop() {
 textFont(font_1);   
   ....                                            //text with font one
 textFont(font_2);
   ....                                            //text with font two                                                 
I should try that actually today in the evening (got to go to work now smiley-sad ).

About the BDC problem, I did not explain properly.
The function works only once overall -
1. I export the app from the processing
2. open the app, press the BDC button, it does what I want with arduino once,
3. I close the app, re-open it, and the BDC does not work any more. It just delays for a few seconds after being pressed and then changes the button colour. I tried adding simple other  tasks to other buttons - to turn on, of led, that works, as many times as I re open the app.

BUT, if I restart the computer, the pressed button executes the function once again for only one time. I can close and open the app as many times as I want, it does not help, although functions in other buttons works perfectly.

Does the app leave some info in the computers memory I dont't know about, that it need restarting.
Or  does the app need some sort of initialization, before executing the BDC function?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Or  does the app need some sort of initialization, before executing the BDC function?
I don't know that the app needs, nor what the computer needs, but what I need is an explanation of the BDC variable, and why you are setting the function up to run only once (and why you are surprised when it runs only once).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[I don't know that the app needs, nor what the computer needs, but what I need is an explanation of the BDC variable, and why you are setting the function up to run only once (and why you are surprised when it runs only once).

hello,

then app I am writing is for data acquisition, for university project. There are two steps, which must be performed before acquiring data, otherwise the machine might be damaged. Firstly the user must do the BDC function, then "load cell" function, and then can start data acquisition. After acquiring the data, the graph will be drawn, and according to the graph, the measured tool is either discarded or repaired.
So to follow the sequence, I thought of those boolean variables, that the sequence BDC > loadCell > Start would be followed.  
The graph produced is purely for quicker visual assessment of the measured tool parameters.
The app will be rarely used, so I want to make it as simple as possible.

I think you do not understand what problem I am having. The problem is that if I run the program, go through all three functions, see the graph and then close/terminate/kill the app (it does not work any more in windows). But if I start it again, the BDC function does not work anymore, event though the app was terminated and all the variables are reset, because the app is restarted. Unless, the computer is restarted, then the app works for one time again  smiley-confuse
« Last Edit: July 16, 2012, 04:17:20 pm by V_king » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But if I start it again, the BDC function does not work anymore, event though the app was terminated and all the variables are reset, because the app is restarted. Unless, the computer is restarted, then the app works for one time again
This implies that you are not setting some variable with a correct initial value. Use print() and println() to see where your program gets to each time it is run.

Frankly, I don't see anything wrong, but I don't have your code or your hardware, so I can't test the code.
Logged

Pages: [1]   Go Up
Jump to: