Hi dear,
I have an ask.How i can show data of speed sensor (magnet) to processing gui?Now i have flash show of data
Processing
import processing.serial.*;
int width = 800;
int height = 600;
String inByte;
int button1X;
int button1Y;
int button1Width = width/10;
int button1Height = height/20;
int button2X;
int button2Y;
int button3X;
int button3Y;
int button2Width = button1Width;
int button2Height = button1Height;
int button3Width = button1Width;
int button3Height = button1Height;
float buttonFont = button1Height/1.5;
float wordFont = button1Height;
Serial arduino;
color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
void setup() {
size(width,height);
smooth();
font = loadFont("CourierNew36.vlw");
buttonColor = color(255);
buttonHighlight = color(200);
buttonPressed = color(50);
button1X = width/10 - button1Width/2;
button1Y = height/5 - button1Height/2;
button2X = 2*width/10 - button1Width/2;
button2Y = height/5 - button1Height/2;
button3X = 3*width/10 - button1Width/2;
button3Y = height/5 - button1Height/2;
arduino = new Serial(this,"COM3", 9600);
arduino.bufferUntil('\n'); // SerialEvent for new line
arduino.clear();
}
void draw() {
update(mouseX,mouseY);
background(0);
if(button1Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button1X,button1Y, button1Width, button1Height);
if(button2Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button2X,button2Y, button2Width, button2Height);
if(button3Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button3X,button3Y, button3Width, button3Height);
textFont(font, buttonFont);
fill(0);
text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3);
text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3);
text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
while (arduino.available () > 0) {
inByte = arduino.readStringUntil('\n');
if ( inByte != null ) {
println(inByte);
fill(125);
text(inByte,width/2, height/2);
}
}
textFont(font, wordFont);
fill(255);
text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2);
}
///////////////////////////////////
void update(int x, int y) {
if(overButton(button1X,button1Y,button1Width,button1Height)){
button1Over = true;
} else {
button1Over = false;
}
if(overButton(button2X,button2Y,button2Width,button2Height)){
button2Over = true;
} else {
button2Over = false;
}
if(overButton(button3X,button3Y,button3Width,button3Height)){
button3Over = true;
} else {
button3Over = false;
}
}
void mousePressed() {
if(button1Over) {
println("Button1 pressed");
arduino.write('S');
fill(buttonPressed);
rect(button1X,button1Y, button1Width, button1Height);
}
if(button2Over) {
println("Button2 pressed");
arduino.write('N');
fill(buttonPressed);
rect(button2X,button2Y, button2Width, button2Height);
}
if(button2Over) {
println("Button3 pressed");
arduino.write('Z');
fill(buttonPressed);
rect(button3X,button3Y, button3Width, button3Height);
}
}
boolean overButton(int x, int y, int width, int height) {
if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
arduino code:
const int pinled=13;
int val;
#define reed A0//pin connected to read switch
//storage variables
int reedVal;
long timer;// time between one full rotation (in ms)
float mph;
float radius = 13.5;// tire radius (in inches)
float circumference;
int pass;
int passs=0;
int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;
void setup(){
Serial.begin(9600);
pinMode(pinled, OUTPUT);
pinMode(A1, INPUT);
reedCounter = maxReedCounter;
circumference = 2*3.14*radius;
pinMode(reed, INPUT);
// TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
//for more info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
reedVal = digitalRead(reed);//get val of A0
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}
void displayMPH(){
Serial.println(mph , BYTE);
delay(3000);
}
void loop()
{
if(Serial.available() >0)
{
val= Serial.read();
if(val == 'S')
{
digitalWrite(pinled, HIGH);
delay(1000);
digitalWrite(pinled, LOW);
}else if(val == 'N')
{
digitalWrite(pinled, HIGH);
}else if(val=='Z')
{
digitalWrite(pinled, LOW);
}
}
//pass = digitalRead(A1);
//Serial.println(pass);
// print mph once a second
displayMPH();
}