action after button pressed for 10 seconds

hi!

I need some help using arduino and processing. i want to make a 3 page program, one start page, one processing page, and one accepted page. I found out with some help how to do it in processing alone using void keyPressed, but i cant figure out how to do it while a button is pressed on my breadboard. if the button is pressed you get the processing page for 10 seconds and the LED on my breadboard should fade. after 10 seconds the "success" page should come up and the led turns off.

import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
Arduino arduino;

int led = 9;
int buttonState = 10;

int fadeAmount = 8;
int opacity = 255;
int circleSize = 75;
int circleYpos = 700;
int circleXpos = 700;
int timer=0;
int count = 10;
int trigger = 0;
int brightness = 0;


boolean activateCircle=false;


void setup() {

  arduino= new Arduino ( this, "COM3", 57600);
  arduino.pinMode ( led, Arduino.OUTPUT);
  arduino.pinMode ( buttonState, Arduino.INPUT);

  size(800, 800); 
  smooth();
  background(250);
  textAlign(CENTER);
  rectMode(CENTER);
  stroke(0);
} 

void draw() {

  if (activateCircle == true) {
    processing();

    if ( (millis()-timer) > 10000 ) {
      circle_off();
    }
  } else {
    startPage();
  }
}

void loop() { 
  if (arduino.digitalRead(buttonState) == Arduino.HIGH) {
    activateCircle=true;
    timer = millis();
  }
}

void startPage() {

  background(250);
  stroke(0);
  fill(255, 2, 2);
  ellipse(width/2, 600, circleSize + 10, circleSize+10);
  ellipse(200, 600, circleSize + 10, circleSize+10);
  ellipse(600, 600, circleSize + 10, circleSize+10);

  fill(0);
  textSize(50);
  text("Start", width/2, 200);
  textSize(30);
  text("Touch the ball to accept the terms of agreement", width/2, height/2 + 50);

  arduino.analogWrite(led, Arduino.LOW);
}

void processing() {

  background(250);
  textSize(15);
  fill(0, opacity);
  text("Sending information", 700, 650);
  fill(255, 2, 2, opacity);
  noStroke();
  ellipse(circleXpos, circleYpos, circleSize, circleSize);

  if ((opacity <0) || (opacity >255))  
    opacity = 255;

  opacity -= fadeAmount;

  fill(250, 2, 2);
  rect(width/2, 235, 600, 100);

  fill(0);
  textSize(30); 
  text("They know who your friends are", 400, 100);
  textSize(40);
  text("Hold your hand on the ball", width/2, 250);
  textSize(25);
  text("They know where you are", 300, 400);
  textSize(20);
  text("They know your interests", 500, 500);
  textSize(42);
  text("Do you agree with this?", width/2- 30, 715);

  arduino.analogWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness <=0 || brightness >= 255) { 
    fadeAmount = -fadeAmount;
  }
  delay(30);
}



void circle_off() {
  background(250);

  fill(0);
  textSize(50);
  text("Agreement success!", width/2, 720);
  fill(250);
  noStroke();
  ellipse(circleXpos, circleYpos, circleSize, circleSize);

  arduino.analogWrite(led, Arduino.LOW);
}

Answers

  • I wouldn't use loop() in line 50 as a name for your function. Also, you never call these lines. Try this partial modified version of your code:

    void draw() {
      checkUserInput();
    
      if (activateCircle == true) {
        processing();
    
        if ( (millis()-timer) > 10000 ) {
          circle_off();
        }
      } else {
        startPage();
      }
    }
    
    void checkUserInput() { 
      if (activateCircle==false && arduino.digitalRead(buttonState) == Arduino.HIGH) {
        activateCircle=true;
        timer = millis();
      }
    }
    

    This is untested code. Related t your LED, its brightness field is a 8 bit field? Just checking....

    You told us what you want your code to do. Additionally, tell us what is your code doing right now, or what it is not doing, as we cannot run your code... we don't have your hardware.

    Kf

Sign In or Register to comment.