Pages: [1]   Go Down
Author Topic: Convert Processing code to Arduino???  (Read 513 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I want to convert the code (below) from Processing into Arduino but I am getting errors with the syntax, basically what I want to do is to get a vibration motor to start, reach maximum speed and slow down again in the shape of a curve. I have it working in processing (using the Arduino Lib) but I would like to have it working also in the Arduino IDE as I want to disconnect the arduino from the computer...any help will be appreciated ...thanks in advance...

Code:
int xspacing = 32;   // How far apart should each horizontal location be spaced
int w;              // Width of entire wave
float theta = 0.0;  // Start angle at 0
float dx;  // Value for incrementing X, a function of period and xspacing
float[] yvalues;  // Using an array to store height values for the wave

void setup() {
  w = 320;
  dx = 0.40212387;
  yvalues = new float[w/xspacing];

}

void loop() {
  background(0);
  Generatewave();
}

void Generatewave() {
  // Increment theta (try different values for 'angular velocity' here
  theta += 0.002;
  // For every x value, calculate a y value with sine function
  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*10.0;
     println(yvalues[i]);
    x+=dx;
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25775
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to convert the code (below) from Processing into Arduino
If instead you say "I want to convert the code (below) from Processing into C/C++", the whole thing becomes much easier.

e.g. not
Code:
float[] yvalues; 
but
Code:
float yvalues [someConstantSize]; 
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Quote
but I am getting errors with the syntax
Share them, you greedy pig!  smiley

Code:
  background(0);
Your Arduino has something to paint the background color on?

Code:
     println(yvalues[i]);
Where are you trying to print to? The Serial Monitor? That requires sending data to the serial port, using the Serial instance.
Logged

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

@AWOL I made that change but when I verify the code below I am now getting the following error: incompatible types in assignment of 'float*' to 'float [10]'

**@PaulS Sorry,I was not being greedy:) and well spotted, I did leave some remnants of Java behind:)...

Code:
int xspacing = 32;   // How far apart should each horizontal location be spaced
int w;              // Width of entire wave
float theta = 0.0;  // Start angle at 0
float dx;  // Value for incrementing X, a function of period and xspacing
//float[] yvalues;  // Using an array to store height values for the wave
float yvalues [10]; 

void setup() {
  w = 320;
  dx = 0.40212387;
  yvalues = new float[10];
  Serial.begin(9600);
}

void loop() {
  Generatewave();
}

void Generatewave() {
  // Increment theta (try different values for 'angular velocity' here
  theta += 0.002;
  // For every x value, calculate a y value with sine function
  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*10.0;
    Serial.println(yvalues[i]);
    x+=dx;
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25775
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
yvalues = new float[10];
If you already declared a size for the array, you don't need/want "new", so delete this line
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: