Pages: [1]   Go Down
Author Topic: Using two FSRs on a timer to switch videos  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The premise here is, if you hit a golf ball, a FSR on the head of the club starts a timer. If within say, 5 seconds, FSR in the cup does not register the ball has gone in it, a video will play telling you "Keep practicing!", ect. I'm having a bit of trouble with the code, especially on the processing side. Any help would be appreciated.

Arduino
Code:
const int clubPin = A0;     
const int cupPin = A1; 

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   int valclub = map(analogRead(clubPin), 0, 1023, 0, 255);
   Serial.println(valclub);
   delay(10);
   int valcup = map(analogRead(cupPin), 0, 1023, 0, 255);
   Serial.println(valcup);
   delay(10);
}

Processing
Code:
import processing.serial.*;
import processing.video.*;
 
float clubValue = 0;        // red value
float cupValue = 0;      // green value
 
Serial myPort;
 
Movie goodOne, goodTwo, goodThree, goodFour, goodFive, goodSix, goodSeven, goodEight, goodNine, goodTen, goodEleven, goodTwelve, goodThirteen, goodFourteen, goodFifteen, goodSixteen, goodSeventeen, badOne, badTwo, badThree, badFour, badFive, badSix, badSeven, badEight, badNine, badTen, badEleven, badTwelve, badThirteen, badFourteen, badFifteen, badSixteen, badSeventeen, badEighteen, badNineteen, badTwenty, badTwentyone, badTwentytwo, badTwentythree, badTwentyfour, badTwentyfive;

void setup() {

  size(1280, 720);
 
  goodOne = new Movie(this, "goodOne.mp4");
  goodTwo = new Movie(this, "goodTwo.mp4");
  goodThree = new Movie(this, "goodThree.mp4");
  goodFour = new Movie(this, "goodFour.mp4");
  goodFive = new Movie(this, "goodFive.mp4");
  goodSix = new Movie(this, "goodSix.mp4");
  goodSeven = new Movie(this, "goodSeven.mp4");
  goodEight = new Movie(this, "goodEight.mp4");
  goodNine = new Movie(this, "goodNine.mp4");
  goodTen = new Movie(this, "goodTen.mp4");
  goodEleven = new Movie(this, "goodEleven.mp4");
  goodTwelve = new Movie(this, "goodTwelve.mp4");
  goodThirteen = new Movie(this, "goodThirteen.mp4");
  goodFourteen = new Movie(this, "goodFourteen.mp4");
  goodFifteen = new Movie(this, "goodFifteen.mp4");
  goodSixteen = new Movie(this, "goodSixteen.mp4");
  goodSeventeen = new Movie(this, "goodSeventeen.mp4");
  badOne = new Movie(this, "badOne.mp4");
  badTwo = new Movie(this, "badTwo.mp4");
  badThree = new Movie(this, "badThree.mp4");
  badFour = new Movie(this, "badFour.mp4");
  badFive = new Movie(this, "badFive.mp4");
  badSix = new Movie(this, "badSix.mp4");
  badSeven = new Movie(this, "badSeven.mp4");
  badEight = new Movie(this, "badEight.mp4");
  badNine = new Movie(this, "badNine.mp4");
  badTen = new Movie(this, "badTen.mp4");
  badEleven = new Movie(this, "badEleven.mp4");
  badTwelve = new Movie(this, "badTwelve.mp4");
  badThirteen = new Movie(this, "badThirteen.mp4");
  badFourteen = new Movie(this, "badFourteen.mp4");
  badFifteen = new Movie(this, "badFifteen.mp4");
  badSixteen = new Movie(this, "badixteen.mp4");
  badSeventeen = new Movie(this, "badSeventeen.mp4");
  badEighteen = new Movie(this, "badEighteen.mp4");
  badNineteen = new Movie(this, "badNineteen.mp4");
  badTwenty = new Movie(this, "badTwenty.mp4");
  badTwentyone = new Movie(this, "badTwentyone.mp4");
  badTwentytwo = new Movie(this, "badTwentytwo.mp4");
  badTwentythree = new Movie(this, "badTwentythree.mp4");
  badTwentyfour = new Movie(this, "badTwentyfour.mp4");
  badTwentyfive = new Movie(this, "badTwentyfive.mp4");
 
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}
 
void draw() {
  // set the background color with the color values:
  background(0);
}
 
void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
 
  if (inString != null) {
  // trim off any whitespace:
  inString = trim(inString);
  // split the string on the commas and convert the
  // resulting substrings into an integer array:
  float[] colors = float(split(inString, ","));
  // if the array has at least three elements, you know
  // you got the whole thing.  Put the numbers in the
  // color variables:
  if (colors.length >=3) {
  // map them to the range 0-255:
  redValue = map(colors[0], 0, 1023, 0, 255);
  greenValue = map(colors[1], 0, 1023, 0, 255);
  }
  }
  }
 

I'm working off a color mixing arduino -> processing sketch so thats why the bottom is redvalues and greenvalues, i have changed these to clubvalues and cup values. whenever the fsr registers a value over 1, that should start or stop the timer. i'm kind of using it as a switch here and not an actual analog sensor. i tried changing the values from 0/1023 to 0/1 in processing, but you need to be pressing it at the hardness threshold to register a 1, so i just left it. thanks guys.
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Processing is expecting two numbers per line separated by a comma I think.  The Arduino isn't generating that format.
Logged

[ I won't respond to messages, use the forum please ]

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

any suggestions then?
Logged

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

i need to make an if statement in processing that says something along the lines of, if timer ends + cup fsr activated, play good video, if timer ends + cup fsr not activated, play bad video. i just don't know how i would go about doing that.
Logged

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

Code:
   int valclub = map(analogRead(clubPin), 0, 1023, 0, 255);
The map function is overkill for dividing a value by 4.

Code:
   delay(10);
Why?

The Arduino is firing off a steady stream of values, every 10 milliseconds. It seems to me that you would want to send a value only when the value signified that an event occurred, like the ball was hit or the ball hit the cup. Of course, you would then need some way for Processing to distinguish what those values meant. Perhaps, it would be better if the club sent a letter, like 'W' whenever it whacked a ball, and the cup would send a 'C' when it caught the ball (if there is only one cup, or a number (different for each cup) if there is more than one cup)).

Your Processing sketch needs some work, too.
Code:
  float[] colors = float(split(inString, ","));
The string being sent by the Arduino now consists of a single int. Why are you converting this to an array of floats?
Logged

Pages: [1]   Go Up
Jump to: