Created
March 31, 2014 18:49
-
-
Save jankolkmeier/9899400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <SVDRest.h> | |
#include <stdlib.h> | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
#define REDPIN 5 | |
#define GREENPIN 6 | |
#define BLUEPIN 3 | |
#define FADESPEED 5 // make this higher to slow down | |
int pins[] = { | |
A0,A1,A2,A3,A4,A5}; // fill in the number of pins to send | |
unsigned long time; | |
unsigned long time1; | |
int q=19; | |
float weight=0; | |
int measure5[20];//array van 20 verschillende metingen | |
int measure1[20]; | |
float verschil =0; | |
float weightgem[6]; | |
float weightsum=0; | |
int k=0; | |
boolean received_score; | |
float compare; | |
long last_sent = 0; | |
// Current score, between 0 and 256; | |
int score = 0; | |
// Load the SVDRest Library: | |
// TYPE EVENTS GLOBALLY UNIQUE ID | |
SVDRest app = SVDRest("SVDScale", "weight,error", "87c7eaed-4112-43bb-8aee-32da70e64dcb"); | |
param set_score_parameters[] = { | |
{ "score", "0-255", "128" }, | |
{ "compare", "-255.0-255.0", "0.0" } | |
}; int set_score_parameters_s = ARRAY_SIZE(set_score_parameters); | |
// Route Callback | |
void set_score(char* data) { | |
lcd.clear(); | |
lcd.setCursor (0,0); | |
lcd.print("Score: "); | |
lcd.print(set_score_parameters[0].value); | |
lcd.setCursor (0,1); | |
lcd.print("Compare: "); | |
lcd.print(set_score_parameters[1].value); | |
// Expect the payload to contain one number, | |
// convert it to int and set the score. | |
//score = atoi(set_score_parameters[0].value); | |
//compare = atof(set_score_parameters[1].value); | |
app.response(true, "0"); | |
received_score = true; | |
delay(5000); | |
} | |
void send_weight() { | |
static char w_buf[5]; | |
// Write measurement to buffer | |
dtostrf(weight,2,1,w_buf); | |
// Emit event 'weight' to server and wait for response. | |
// NAME, DATA, TIMEOUT, ATTEMPTSf | |
int response_code = app.emit("weight", w_buf, 1200, 3); | |
// Debug | |
if (response_code == 200) { | |
//Serial.print("Successfully emitted event!"); | |
} else { | |
int a = 5; | |
while (a-- > 0) { | |
lcd.clear(); | |
lcd.setCursor (0,0); | |
lcd.print("Error"); | |
lcd.setCursor (0,1); | |
lcd.print("Please try again"); | |
} | |
//Serial.print("No satisfying response code: "); | |
//Serial.println(response_code); | |
} | |
} | |
//Einde deel 1/3 van gedeelte van Jan | |
// initialize the library with the numbers of the interface pins | |
void setup() { | |
//Begin deel 2/3 van gedeelte van Jan | |
Serial.begin(57600); | |
// Register Routes | |
// LOCATION, FUNCTION, PARAMS, SIZE | |
app.POST("/score", &set_score, set_score_parameters, set_score_parameters_s); | |
//Einde deel 2/3 van gedeelte van Jan | |
// set up the LCD's number of columns and rows: | |
lcd.begin(16, 2); | |
// Print a message to the LCD. | |
lcd.print("Gewicht:"); | |
pinMode(REDPIN, OUTPUT); | |
pinMode(GREENPIN, OUTPUT); | |
pinMode(BLUEPIN, OUTPUT); | |
} | |
void LCDPrintFloat( float f){ | |
if (f<10) lcd.print("0"); | |
lcd.print((int)f); | |
lcd.print("."); | |
int temp = (f - (int)f) * 10; | |
lcd.print( abs(temp) ); | |
lcd.print(" "); | |
} | |
void loop(){ | |
// Advertise Device to Gateway | |
if (app.ready()) app.advertise(); | |
// Parse XBee data for SVDRest | |
if (Serial.available()) app.parse_data(Serial.read()); | |
/* | |
if (last_sent+10000 > millis()) { | |
measure(); | |
}*/ | |
} | |
void measure() { | |
if (millis()>time+100){ // more acurate than delay(50); | |
time=millis(); // store timer for the next time | |
measure5[q] = analogRead(pins[5]); | |
//elke keer dat de loop wordt doorlopen wordt er een input waarde aan de measure array toegevoegd | |
measure1[q] = analogRead(pins[1]); | |
q++; | |
if(q>19) q=0; | |
if (q==0) { | |
calculate_response(); | |
} | |
} | |
} | |
void calculate_response() { | |
float sum5 = 0; | |
float sum1 = 0; | |
float weight5 =0; | |
float weight1 = 0; | |
for (int n = 0; n < 20; n++) { | |
sum5+=measure5[n];// Telt alle 20 entities van measure[] bij elkaar op | |
sum1+=measure1[n]; | |
} | |
weight1 = (sum1/20); | |
weight5 = (sum5/20);// Slaat het gemiddelde van 20 metingen op | |
weight = 0.244*((weight1+weight5)/2)-1.35 ; //Conversie van meetwaarde naar gewicht in kg | |
lcd.setCursor(0, 1); //Verplaatst cursor naar tweede regel. | |
if (weight<100){ | |
lcd.print("0"); | |
} | |
if (weight>30) { | |
LCDPrintFloat(weight); | |
lcd.print("kg ("); | |
lcd.print(k); | |
lcd.print(")"); | |
weightgem[k] = weight; | |
k++; | |
if (k>5) { | |
for (int b=1; b<6; b++) { | |
weightsum += weightgem[b]; | |
} | |
weight = weightsum/5;//Neemt het gemiddelde van de weergegeven metingen. | |
lcd.setCursor(0, 1); | |
if (weight<100){ | |
lcd.print("0"); | |
} | |
LCDPrintFloat(weight); | |
send_weight(); | |
last_sent = millis(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment