Is there any functions that are prior than others?

edited May 2014 in Arduino

I'm using an arduino to get data from an accelerometer. The goal of the program is to execute two methods when the sensor detects a certain movement. One of the functions turns off some leds and turns them on, one by one, using delay to make it last for a while. The other function is used to display a certain image on the screen each time that movement is detected, what means, the same movement triggers both methods.

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int[] ledPins = {6,2,3,4,5};
int sensor = 0;
int read;
boolean energy = true;
int step = 0;
int pX = 400;
int pY = 500;
int size = 100;

void setup()
{
  size(1024,768);
  imageMode(CENTER);
  background(loadImage("bg.jpg"));
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 57600);
  for (int i = 0; i < 5; i++)
  {
  arduino.pinMode(ledPins[i], Arduino.OUTPUT);
  }
  arduino.pinMode(sensor, Arduino.INPUT);
}

void draw()
{
  for (int i = 0; i < 5; i++)
  {
  arduino.digitalWrite(ledPins[i], Arduino.HIGH);
  }
  read = arduino.analogRead(sensor);

  if (read>410)
  {
    step ++;
    energy = false;
  }
    steps(); 
    energy();
}

the variable step is set to 0, so when read>410, step changes to 1 which should trigger this:

void steps()
{
  if (step == 1)
    {

      background(loadImage("bg1.jpg"));

    }
}

the problem, is that the program executes first the function energy() and only after finishing, it sets the desired background. Here's the code of the energy function:

void energy()
{
  if (energy == false)

  {
    for (int i = 0; i < 5; i++)
    {
    arduino.digitalWrite(ledPins[i], Arduino.LOW);
    }

    for (int i = 0; i < 5; i++)
    {
      delay(500);
      arduino.digitalWrite(ledPins[i], Arduino.HIGH);
      delay(2000);
    }

    energy = true;
 }
}

My question is, why both are not triggered at the same time? Or, if that is not possible, what should I do to display the image first? Thanks in advance.

Answers

  • _vk_vk
    edited May 2014 Answer ✓

    select code and hit "control k" to format it in the forum and allow people to read and copy it ;)

  • edited May 2014

    Thanks _vk! I hope the code is well shown now. By mistake I clicked your answer as accepted answer and I cannot find an option to undo that. I hope you or someone else can still help me with this. I also included more code just in case someone want to try it to test.

  • Answer ✓

    only after finishing, it sets the desired background.

    yes, this is how processing works. the screen is only updated at the end of the draw(). your draw() calls energy() which calls delay() which stops everything.

    the reference used to say: "Forces the program to stop running for a specified time. Delay times are specified in thousandths of a second, therefore the function call delay(3000) will stop the program for three seconds. Because the screen is updated only at the end of draw(), the program may appear to "freeze", because the screen will not update when the delay() method is used. This function has no affect inside setup()."

    however, i now notice that delay() has been removed from the reference, probably because it is misused so often.

  • edited May 2014 Answer ✓

    Function delay() should only be used outside the "Animation" Thread!
    Take notice Processing provides a hidden function called thread("") which allows invoking another function as a separate Thread!
    Therefore, delay() would block only that Thread which is currently executing it; not affecting the others! ;;)

  • Thank you koogs for clarifying what was going on, now I understand what was happening when using the delay(). And thank you so much GoToLoop for providing a solution! ;;)

Sign In or Register to comment.