Sensor waardes op TFT weergeven

Arduino specifieke Software
Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Sensor waardes op TFT weergeven

Berichtdoor benovitch » 04 Nov 2015, 09:08

Voor mijn model vliegtuigen ben ik bezig met het maken van een Center of Gravity meter. Deze wordt gebruikt om het gewicht van het model te wegen en eveneens het juiste zwaartepunt te berekenen. Ik heb reeds een werkende set maar dan met een 20*4 lcd. Nu ben ik bezig met een set met TFT met touchscreen. Ik heb een menu structuur gemaakt, JOS, aan de hand van een voorbeeld dat ik gevonden had op het net. De weer te geven tekst, afbeeldingen is geen probleem. Echter wanneer ik nu live data van een sensor wil weergeven gaat het mis. Voorlopig gebruik ik een trimpot op een analoge poort voor het gemak. Nadien wordt er een load cell met HX711 aangesloten om het eigenlijk gewicht te meten.
Wanneer ik nu in mijn code een extra functie schrijf waar ik dan zeg waar ik de waarde wil op het display enz. Wanneer ik deze dan oproep in via het scherm gewicht meten komt de waarde er 1 sec op en dan verdwijnt ze weer. Heb al verschillende dingen geprobeerd niets blijkt te helpen.
Zal deze middag de code uploaden die ik geschreven/aangepast heb.
Wanneer ik kijk naar het voorbeeld op de Arduino site van hoe ze een analoge waarde weergeven op de TFT zie ik dat deze in de main loop staat. Wanneer ik dit doe komt de waarde er constant op en dat is niet de bedoeling. Ook werkt dan de touchscreen functie niet meer.


Ben

Advertisement

Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Re: Sensor waardes op TFT weergeven

Berichtdoor benovitch » 04 Nov 2015, 18:10

Hier is de code. Onderaan de code staat de functie Void Gewicht(). Deze wordt opgeroepen via de functie Weight. Wanneer ik op het startscherm op de knop Weight druk ga ik naar een ander scherm (page 1) in deze sketch. Alles komt er op wat er moet opkomen, enkel de waarde van de sensor komt er een dikke second op en verdwijnt dan.
Iemand hier een oplossing voor?

cpp code
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <EEPROM.h>
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 22
D1 connects to digital 23
D2 connects to digital 24
D3 connects to digital 25
D4 connects to digital 26
D5 connects to digital 27
D6 connects to digital 28
D7 connects to digital 29
For Mega's use pins 22 thru 29 (on the double header at the end)
*/

#define YP A5 // must be an analog pin, use "An" notation! A1 for shield
#define XM A6 // must be an analog pin, use "An" notation! A2 for shield
#define YM 32 // mega=32
#define XP 33 // mega=33

#define TS_MINX 115
#define TS_MINY 77
#define TS_MAXX 906
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 260);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET -1
// Color definitions - in 5:6:5
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define TEST 0x1BF5
#define JJCOLOR 0x1CB6
#define JJORNG 0xFD03

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int i = 0;
int page = 0;
int blv;
int sleep = 0;
int esleepeeprom;
int blveeprom;
int pulsev = 0;
int redflag = 0;
int greenflag = 0;
int backlight = 10; //pin-10 uno pin-35 mega pin-3 for shield if you have done the MOD for PWM backlight
int battfill;
unsigned long sleeptime;
unsigned long battcheck = 10000; // the amount of time between voltage check and battery icon refresh
unsigned long prevbatt;

int battsens;
int battv;
int battold;
int battpercent;
int barv;
int prevpage;
int sleepnever;
int esleep;
int backlightbox;
unsigned long awakeend;
unsigned long currenttime;
unsigned long ssitime;
char voltage[10];
char battpercenttxt[10];
char Weight[10];

void setup(void) {

//analogReference(5);
esleep = EEPROM.read(1);
blv = EEPROM.read(2);
if (esleep == 0 && blv == 0) { // if no previous saves then set sleep and backlight to default.
EEPROM.write(1, 4);
EEPROM.write(2, 200);
}
if (esleep == 1) {
sleeptime = 10000;
}
if (esleep == 2) {
sleeptime = 20000;
}
if (esleep == 3) {
sleeptime = 30000;
}
if (esleep == 4) {
sleeptime = 60000;
}
if (esleep == 5) {
sleeptime = 120000;
}
if (esleep == 6) {
sleeptime = 300000;
}
if (esleep == 7) {
sleeptime = 600000;
}
if (esleep == 8) {
sleepnever = 1;
}
awakeend = sleeptime + 30000; // set the current sleep time based on what the saved settings in EEPROM were
pinMode(backlight, OUTPUT);
Serial.begin(9600);
Serial.println("JOS");
Serial.println("Jeremy Saglimbeni (c) 2011");

tft.reset();

tft.begin(0x8357); //<---Here I bypassed the above. cause I already checked...
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-==-

tft.setRotation(1); //sets landscape mode
tft.fillScreen(BLACK);
tft.drawRect(50, 40, 380, 240, WHITE);
tft.fillRect(51, 41, 378, 238, JJCOLOR);
for (i = 0; i <= blv; i += 1) {
analogWrite(backlight, i);
delay(2);
}
delay(2);
tft.setTextSize(10);
tft.setTextColor(WHITE);
tft.setCursor(190, 45);
tft.println("CG");
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(200, 130);
tft.println("Meter");
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(233, 205);
tft.println("by");
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(120, 240);
tft.println("Benovitch 2015");
delay(500); //introscreen delay
tft.fillScreen(BLACK);
tft.fillRect(0, 0, 480, 20, JJCOLOR);
tft.drawRect(445, 2, 30, 16, WHITE); //battery body
tft.fillRect(475, 6, 4, 8, WHITE); // battery tip
tft.fillRect(446, 3, 28, 14, BLACK); // clear the center of the battery
drawbatt();
homescr(); // draw the homescreen

}
#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop() {

currenttime = millis();
unsigned long currentawake = millis();
if ((currentawake > awakeend) && (sleepnever == 0)) {
if (sleep == 0) {
for (i = blv; i >= 0; i -= 1) {
analogWrite(backlight, i);
delay(4);
}
sleep = 1;
}
}

TSPoint p = ts.getPoint();

if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
awakeend = currenttime + sleeptime; //set the sleep time when screen is pressed
if (sleep == 1) { // if asleep, then fade the backlight up
for (i = 0; i <= blv; i += 1) {
analogWrite(backlight, i);
delay(1);
}
sleep = 0; // change the sleep mode to "awake"
return;
}

// turn from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 480); //default is (480, 0) [default puts touch cord. 0=x/y upper right.
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); //default is (320, 0) [I change these cause i like 0=xy bottom left.

Serial.print("p.y:"); // this code will help you get the y and x numbers for the touchscreen
Serial.print(p.y);
Serial.print(" p.x:");
Serial.println(p.x);

//WEIGHT Button
if (p.y > 12 && p.y < 75 && p.x > 24 && p.x < 71) {
if (page == 1) {
m1b2action(); // link to action
}
if (page == 0) { // if you are on the "home" page (0)
page = 1; // then you just went to the first page
redraw(); // redraw the screen with the page value 1, giving you the page 1 menu
}
}
//CoG Button
if (p.y > 88 && p.y < 148 && p.x > 24 && p.x < 71) {
if (page == 1) {

}
if (page == 0) { // if you are on the "home" page (0)
page = 2; // then you just went to the 2e page
redraw(); // redraw the screen with the page value 1, giving you the page 1 menu
}
}
//BALANCE Button
if (p.y > 163 && p.y < 226 && p.x > 24 && p.x < 71) {
if (page == 3) {
m3b1action();
}
if (page == 0) { // if you are on the "home" page (0)
page = 3; // then you just went to the 3e page
redraw(); // redraw the screen with the page value 1, giving you the page 1 menu
}
}
//SETTINGS Button
if (p.y > 241 && p.y < 297 && p.x > 24 && p.x < 71) {
if (page == 4) {
m4b1action();
}
if (page == 0) { // if you are on the "home" page (0)
page = 4; // then you just went to the 4e page
redraw(); // redraw the screen with the page value 1, giving you the page 1 menu
}
}

// TARE button
if (p.y > 241 && p.y < 297 && p.x > 24 && p.x < 71) {
if (page == 1) {
delay(200);
m1b1action();
}
}

// home
if (p.y > 288 && p.y < 312 && p.x > 392 && p.x < 443) { // if the home icon is pressed
if (page == 4) { // if you are leaving the settings page
//clearmessage(); // clear the battery voltage out of the message box
clearstatus();
esleepeeprom = EEPROM.read(1);
blveeprom = EEPROM.read(2);
if (esleep != esleepeeprom || blv != blveeprom) {
EEPROM.write(1, esleep);
EEPROM.write(2, blveeprom);

tft.setCursor(20, 3);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.println("Settings Saved"); // display settings saved in message box
} else {

tft.setCursor(20, 3);
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.println("No Changes Made");
}
}
if (page = prevpage)
;
{ // a value to keep track of what WAS on the screen to redraw/erase only what needs to be
page = 0; // make the current page home
redraw(); // redraw the page
}
}

// backlight buttons
if (p.y > 4 && p.y < 36 && p.x > 384 && p.x < 447) { //-
if (page == 4) {
blightdown();
}
}
if (p.y > 173 && p.y < 208 && p.x > 387 && p.x < 447) { //+
if (page == 4) {
blightup();
}
}
// sleep buttons
if (p.y > 4 && p.y < 33 && p.x > 296 && p.x < 361) {
if (page == 4) {
sleepdec();
}
}
if (p.y > 173 && p.y < 210 && p.x > 296 && p.x < 361) {
if (page == 4) {
sleepinc();
}
}
/*
// optional buttons
if (p.y > 1 && p.y < 50 && p.x > 52 && p.x < 100) {
if (page == 6) {
option3down();
}
}
if (p.y > 259 && p.y < 315 && p.x > 52 && p.x < 100) {
if (page == 6) {
option3up();
}
}
*/

}

if (currenttime - prevbatt > battcheck) {
drawbatt();
prevbatt = currenttime;

}
/*if(currenttime - prevWeight > Weightsenscheck){
drawWeightsens();
prevWeight = currenttime;
}*/

}

void redraw() { // redraw the page
if ((prevpage != 4) || (page != 5)) {
//clearcenter();
}
if (page == 0) {
homescr();
}
if (page == 1) {
WEIGHT();
}
if (page == 2) {
CoG();
}
if (page == 3) {
BALANCE();
}
if (page == 4) {
settingsscr();
}

}
void clearcenter() { // the reason for so many small "boxes" is that it's faster than filling the whole thing
tft.fillRect(0, 20, 480, 250, BLACK);
}
void clearstatus() { // this is used to erase the extra drawings when exiting the settings page
tft.fillRect(0, 0, 444, 20, JJCOLOR);
}
void homescr() {

clearcenter();
clearstatus();
drawWeightbutton();
drawCoGbutton();
drawBalancebutton();
drawSettingsbutton();

}
void WEIGHT() {

tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20, 3);
tft.println("Weight");
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.setCursor(10, 40);
tft.println("Nose Gear: ");
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.setCursor(10, 80);
tft.println("Main Gear L: ");
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.setCursor(10, 120);
tft.println("Main Gear R: ");
tft.setTextColor(BLUE);
tft.setTextSize(4);
tft.setCursor(10, 160);
tft.println("Total Weight:");
tft.setTextColor(JJORNG);
tft.setTextSize(4);
tft.setCursor(20, 205);
//tft.println(Weight);
tft.drawRect(360, 270, 100, 40, WHITE);
tft.fillRect(361, 271, 98, 38, YELLOW);
tft.setTextColor(RED);
tft.setTextSize(3);
tft.setCursor(375, 279);
tft.println("TARE");
tft.fillRect(20, 270, 100, 40, BLACK);
tft.fillRect(133, 270, 100, 40, BLACK); // Fill other buttons (CoG, Balance, Setttings)
tft.fillRect(247, 270, 100, 40, BLACK);
drawhomebutton();
Gewicht();
}

void CoG() {
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20, 3);
tft.println("CoG");
tft.fillRect(20, 270, 100, 40, BLACK);
tft.fillRect(133, 270, 100, 40, BLACK);
tft.fillRect(247, 270, 100, 40, BLACK);
tft.fillRect(360, 270, 100, 40, BLACK);
drawhomebutton();
}
void BALANCE() {
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20, 3);
tft.println("Balance");
tft.fillRect(20, 270, 100, 40, BLACK);
tft.fillRect(133, 270, 100, 40, BLACK);
tft.fillRect(247, 270, 100, 40, BLACK);
tft.fillRect(360, 270, 100, 40, BLACK);
drawhomebutton();
}

void settingsscr() {

// backlight level
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20, 3);
tft.println("Settings");
tft.fillRect(0, 20, 60, 50, RED);
tft.drawRect(0, 20, 60, 50, WHITE);
tft.drawRect(80, 20, 160, 50, JJCOLOR);
tft.fillRect(260, 20, 60, 50, GREEN);
tft.drawRect(260, 20, 60, 50, WHITE);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.setCursor(22, 33);
tft.println("-");
tft.setTextSize(1);
tft.setCursor(120, 31);
tft.println("Backlight Level");
tft.setTextSize(3);
tft.setCursor(282, 33);
tft.println("+");
tft.drawRect(110, 48, 100, 10, WHITE);
blbar();

// sleep time
tft.fillRect(0, 80, 60, 50, RED);
tft.drawRect(0, 80, 60, 50, WHITE);
tft.drawRect(80, 80, 160, 50, JJCOLOR);
tft.fillRect(260, 80, 60, 50, GREEN);
tft.drawRect(260, 80, 60, 50, WHITE);
tft.setTextSize(3);
tft.setCursor(22, 93);
tft.println("-");
tft.setTextSize(2);
tft.setCursor(100, 91);
tft.println("Sleep Time");
tft.setTextSize(3);
tft.setCursor(282, 93);
tft.println("+");
showsleep();
tft.fillRect(20, 270, 100, 40, BLACK);
tft.fillRect(133, 270, 100, 40, BLACK);
tft.fillRect(247, 270, 100, 40, BLACK);
tft.fillRect(360, 270, 100, 40, BLACK);
drawhomebutton();

}
/*
void decimate(char test[],int dec) { // creates decimal function decimate(string containing integer, number of decimal places)

int i=0;
int length=strlen(test);
char msg[10]="";

strcpy(msg,test);

if (length <= dec) {
for(i=dec;i>(dec-length);i--) msg[i] = msg[i-(dec-length+1)];
for(i=0;i<(dec+1-length);i++) msg[i]='0';
length = strlen(msg);
}
for (i=length;i>(length-dec);i--) msg[i]=msg[i-1];
msg[length-dec]='.';

strcpy(test,msg);
} */

void sleepinc() { // sleep increese adjustment
if (sleeptime == 600000) {
sleepnever = 1;
esleep = 8;
sleeptime = 11111111;
showsleep();
}
if (sleeptime == 300000) {
sleeptime = 600000;
esleep = 7;
showsleep();
}
if (sleeptime == 120000) {
sleeptime = 300000;
esleep = 6;
showsleep();
}
if (sleeptime == 60000) {
sleeptime = 120000;
esleep = 5;
showsleep();
}
if (sleeptime == 30000) {
sleeptime = 60000;
esleep = 4;
showsleep();
}
if (sleeptime == 20000) {
sleeptime = 30000;
esleep = 3;
showsleep();
}
if (sleeptime == 10000) {
sleeptime = 20000;
esleep = 2;
showsleep();
}
delay(350);
}
void sleepdec() { // sleep decreese adjustment
if (sleeptime == 20000) {
sleeptime = 10000;
esleep = 1;
showsleep();
}
if (sleeptime == 30000) {
sleeptime = 20000;
esleep = 2;
showsleep();
}
if (sleeptime == 60000) {
sleeptime = 30000;
esleep = 3;
showsleep();
}
if (sleeptime == 120000) {
sleeptime = 60000;
esleep = 4;
showsleep();
}
if (sleeptime == 300000) {
sleeptime = 120000;
esleep = 5;
showsleep();
}
if (sleeptime == 600000) {
sleeptime = 300000;
esleep = 6;
showsleep();
}
if (sleepnever == 1) {
sleeptime = 600000;
sleepnever = 0;
esleep = 7;
showsleep();
}
delay(350);
}
void showsleep() { // shows the sleep time on the settings page
tft.fillRect(110, 108, 80, 10, BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(1);
if (sleeptime == 10000) {
tft.setCursor(130, 108);
tft.println("10 Seconds");
}
if (sleeptime == 20000) {
tft.setCursor(130, 108);
tft.println("20 Seconds");
}
if (sleeptime == 30000) {
tft.setCursor(130, 108);
tft.println("30 Seconds");
}
if (sleeptime == 60000) {
tft.setCursor(136, 108);
tft.println("1 Minute");
}
if (sleeptime == 120000) {
tft.setCursor(133, 108);
tft.println("2 Minutes");
}
if (sleeptime == 300000) {
tft.setCursor(133, 108);
tft.println("5 Minutes");
}
if (sleeptime == 600000) {
tft.setCursor(130, 108);
tft.println("10 Minutes");
}
if (sleepnever == 1) {
tft.setCursor(133, 108);
tft.println("Always On");
}
}
void option3down() { // adjust option 3 down in the settings screen
}
void option3up() { // adjust option 3 up in the settings screen
}
//custom defined actions - this is where you put your button functions
void m1b1action() {
Serial.println("TARE");
}
void m1b2action() {

}
void m1b3action() {
}
void m1b4action() {
}
void m1b5action() {
}
void m1b6action() {
}
void m2b1action() {
}
void m2b2action() {
}
void m2b3action() {
}
void m2b4action() {
}
void m2b5action() {
}
void m2b6action() {
}
void m3b1action() {
}
void m3b2action() {
}
void m3b3action() {
}
void m3b4action() {
}
void m3b5action() {
}
void m3b6action() {
}
void m4b1action() {
}
void m4b2action() {
}
void m4b3action() {
}
void m4b4action() {
}
void m4b5action() {
}
void m4b6action() {
}
void m5b1action() {
}
void m5b2action() {
}
void m5b3action() {
}
void m5b4action() {
}
void m5b5action() {
}
void m5b6action() {
}

void blightup() { // increase the backlight brightness
blv = blv + 5;
if (blv >= 255) {
blv = 255;
}
analogWrite(10, blv);
blbar();
}
void blightdown() { // decrease the backlight brightness
blv = blv - 5;
if (blv <= 5) {
blv = 5;
}
analogWrite(10, blv);
blbar();
}
void blbar() { // this function fills the yellow bar in the backlight brightness adjustment
if (blv < barv) {
tft.fillRect(111, 49, 98, 8, BLACK);
}
backlightbox = map(blv, 1, 255, 0, 98);
tft.fillRect(111, 49, backlightbox, 8, YELLOW);
barv = blv;
delay(25);
}

void drawhomebutton() {
tft.drawRect(438, 22, 40, 40, WHITE);
tft.fillRect(439, 23, 38, 38, RED);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.setCursor(448, 28);
tft.println("X");
}

void drawBalancebutton() {
tft.drawRect(247, 270, 100, 40, WHITE);
tft.fillRect(248, 271, 98, 38, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(257, 284);
tft.println("BALANCE");
}

void drawCoGbutton() {
tft.drawRect(133, 270, 100, 40, WHITE);
tft.fillRect(134, 271, 98, 38, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.setCursor(157, 279);
tft.println("CoG");
}
void drawWeightbutton() {
tft.drawRect(20, 270, 100, 40, WHITE);
tft.fillRect(21, 271, 98, 38, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(35, 284);
tft.println("WEIGHT");
}

void drawSettingsbutton() {
tft.drawRect(360, 270, 100, 40, WHITE);
tft.fillRect(361, 271, 98, 38, YELLOW);
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(363, 284);
tft.println("SETTINGS");
}

void clearmessage() {
tft.fillRect(12, 213, 226, 16, BLACK); // black out the inside of the message box
}
void drawbatt() {
battv = analogRead(A10); // read the voltage
//battsens = map(battv,0 , 1023, 0, 5);
if (battv < battold) { // if the voltage goes down, erase the inside of the battery
tft.fillRect(446, 3, 28, 14, BLACK);

}
battfill = map(battv, 0, 1023, 2, 28); // map the battery voltage 300 is the low, 415 is the high
battfill = constrain(battfill, 2, 28); // constrains batt display limits
if (battfill > 8) { // if the battfill value is between 8 and 18, fill with green
tft.fillRect(446, 3, battfill, 14, GREEN);
//Serial.println(battfill);
} else { // if the battfill value is below 8, fill with red
tft.fillRect(446, 3, battfill, 14, RED);
}
battold = battv; // this helps determine if redrawing the battfill area is necessary
}
void Gewicht() {
float Weightsens = analogRead(A8);
float Weightsensmap = map(Weightsens, 0, 1024, 0, 10000);
itoa(Weightsensmap, Weight, 10);
//Serial.println(Weightsens);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.setCursor(390, 40);
tft.print(Weight);
delay(1000);
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(390, 40);
tft.print(Weight);

}
/*
void drawbattvoltage() {
battv = analogRead(A10); // read the voltage ANalog 1
battpercent = (battv / 480) * 100, 2; // if using battery set 480(4.80v) to fresh charge value
battpercent = constrain(battpercent, 1, 100); // battery percent between 1% and 100%
itoa(battpercent, battpercenttxt, 10);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.setCursor(390, 4);
tft.println(battpercenttxt);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.setCursor(430, 4);
tft.println("%");

}
void drawbattpercent() {

battv = analogRead(A10); // read the voltage Analog 10
battpercent = map(battv, 0, 1024, 0, 100);
//battpercent = (battv / 480) * 100, 2; // if using battery set 480(4.80v) to fresh charge value
battpercent = constrain(battpercent, 1, 100); // battery percent between 1% and 100%
itoa(battpercent, battpercenttxt, 10);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.setCursor(390, 4);
tft.println(battpercenttxt);
delay(250);
tft.setTextColor(JJCOLOR);
tft.setTextSize(2);
tft.setCursor(390, 4);
tft.println(battpercenttxt);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.setCursor(430, 4);
tft.println("%");
} */


@edit: moderator: Code ff beter leesbaar

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Sensor waardes op TFT weergeven

Berichtdoor shooter » 04 Nov 2015, 22:10

ik kom er niet doorheen helaas, even een beetje herschrijven is handig:
gebruik voor tijden nooit delay.
zet in je loop alleen een paar functieaanroepen, zoals touch, scherm, meten etc.
ik zet in elke functie bovenaan, een tijdklok (blink without delay) als de routine niet hoeft dan return.
define elk getal en gebruik die namen in je programma.
ook pinnamen
bedenk bij elke regel of deze nodig is, bijv constrain na een map van 0 tot 1023 is te veel.
paul deelen
shooter@home.nl

Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Re: Sensor waardes op TFT weergeven

Berichtdoor benovitch » 05 Nov 2015, 08:54

Shooter,

Ik denk dat er al iets gelijkaardig in staat voor het volume van het batterij icoontje constant te updaten:

if (currenttime - prevbatt > battcheck) {
drawbatt();
prevbatt = currenttime;

}

Aangezien het batterij icoontje constant te zien is in alle schermen, is het dan best dat ik een gelijkaardig stukje code schrijf maar dan plaats in het stuk void Gewicht(){} zodat ik deze enkel te zien krijg tijdens het oproepen van de functie?

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Sensor waardes op TFT weergeven

Berichtdoor shooter » 05 Nov 2015, 10:19

ja zeker, en in de loop zet je dan currenttime=millis();
die current gebruik je dan overal.
in de examples staat een goed voorbeeld hoe je meerdere tijden kunt gebruiken, want voor elke functie heb je natuurlijk andere timers nodig.

en het display doe je in een aparte functie met teksten die je in bijv gewicht een goede waarde geeft.
dan krijg je dus zoiets als
void display1(){
print(positie,tekst);
....
}
om flikkeren te voorkomen hoef je alleen te schrijven als er iets verandert is.
paul deelen
shooter@home.nl

Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Re: Sensor waardes op TFT weergeven

Berichtdoor benovitch » 06 Nov 2015, 10:29

ik heb er nu zo'n extra tijdklok in gestoken

if (currenttime - prevweight > weightcheck){
prevweight = currenttime;
Gewicht;
}

Wanneer ik deze dan upload dan komt van in het begin de sensorwaarde op het scherm en niet wanneer ik het Weight scherm oproep.

Deze is dan void Gewicht

Code: Alles selecteren
void Gewicht(){
Weightsens = analogRead(A8);
Weightsensmap = map(Weightsens,0, 1023, 0, 1000);
itoa (Weightsensmap,Weight,10)

if (Weightsensmap != Weightold){
tft.setTextSize(2);
tft.setCursor(20,240);
tft.print(Weight);
delay (10);
tft.fillRect(20,240,100,20,BLACK);
tft.setCursor(20,240);
tft.setTextColor(WHITE);
tft.print(WEIGHT);
}
Weightold = Weightsensmap;
}
 


De sensorwaarde wordt nu enkel geupdate wanneer er een verandering is in waarde.

Zou het kunnen dat ik voor de timer iets moet schrijven die zegt: alleen de timer uitvoeren wanneer pagina 2 (WEIGHT) true is.

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Sensor waardes op TFT weergeven

Berichtdoor shooter » 06 Nov 2015, 20:41

nee je oet wel telkens controleren of het gewicht niet verandert is echter jouw aanroep van weight moet wel zijn weight ();
en de onderste regel weightold moet 1 regel omhoog want dat moet alleen als er verschil is, nu komt er nooit verschil.
paul deelen
shooter@home.nl

Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Re: Sensor waardes op TFT weergeven

Berichtdoor benovitch » 06 Nov 2015, 22:19

Dus van dit:
Code: Alles selecteren
void Gewicht(){
Weightsens = analogRead(A8);
Weightsensmap = map(Weightsens,0, 1023, 0, 1000);
itoa (Weightsensmap,Weight,10)

if (Weightsensmap != Weightold){
tft.setTextSize(2);
tft.setCursor(20,240);
tft.print(Weight);
delay (10);
tft.fillRect(20,240,100,20,BLACK);
tft.setCursor(20,240);
tft.setTextColor(WHITE);
tft.print(WEIGHT);
}
Weightold = Weightsensmap;
}


Maken we dit:
Code: Alles selecteren
void Gewicht(){
Weightsens = analogRead(A8);
Weightsensmap = map(Weightsens,0, 1023, 0, 1000);
itoa (Weightsensmap,Weight,10)

if (Weightsensmap != Weightold)
tft.setTextSize(2);
tft.setCursor(20,240);
tft.print(Weight);
delay (10);
tft.fillRect(20,240,100,20,BLACK);
tft.setCursor(20,240);
tft.setTextColor(WHITE);
tft.print(WEIGHT);

Weightold = Weightsensmap;
   }
}


Waarom moet mijn aanroep weight(); zijn?
Is het niet voldoende om de void Gewicht(); aan te roepen in mijn void WEIGHT?
Code: Alles selecteren
void WEIGHT() {
 
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(20, 3);
    tft.println("Weight");
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.setCursor(10, 40);
    tft.println("Nose Gear: ");
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.setCursor(10, 80);
    tft.println("Main Gear L: ");
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.setCursor(10, 120);
    tft.println("Main Gear R: ");
    tft.setTextColor(BLUE);
    tft.setTextSize(4);
    tft.setCursor(10, 160);
    tft.println("Total Weight:");
    tft.setTextColor(JJORNG);
    tft.setTextSize(4);
    tft.setCursor(20, 205);
    //tft.println(Weight);
    tft.drawRect(360, 270, 100, 40, WHITE);
    tft.fillRect(361, 271, 98, 38, YELLOW);
    tft.setTextColor(RED);
    tft.setTextSize(3);
    tft.setCursor(375, 279);
    tft.println("TARE");
    tft.fillRect(20, 270, 100, 40, BLACK);
    tft.fillRect(133, 270, 100, 40, BLACK); // Fill other buttons (CoG, Balance, Setttings)
    tft.fillRect(247, 270, 100, 40, BLACK);
    drawhomebutton();
    Gewicht();
}


Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Sensor waardes op TFT weergeven

Berichtdoor nicoverduin » 07 Nov 2015, 09:45

Even wat C/C++ termen om nog meer verwarring te voorkomen.....

void = niets.
een functie wordt beschreven als
cpp code
xxxx functie(yyy, zzz) {

statements

}


xxxx is wat de functie teruggeeft. Dat kan een int, char, byte, bool of wat dan ook zijn. het kan ooi een void zijn. Dat laatste betekent dat er niets wordt teuggegeven

yyy en zzz kunnen parameters zijn die aan de functie worden doorgegeven. yyy zou bijv. int getal kunnen zijn. Dat betekent dat er een getal van het type int wordt doorgegeven.

Als je een functie aanroept dan roep je die als volgt aan:

cpp code
//
// aanroepen functie
//
functie(2,3);
}


De functie wordt aangeroepen met 2 parameters (2 en 3) en er komt niets terug uit deze functie.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 24
Geregistreerd: 01 Sep 2013, 13:00

Re: Sensor waardes op TFT weergeven

Berichtdoor benovitch » 07 Nov 2015, 19:41

Nico,

Bedankt vor je reactie maar al die programmeertaal is meestal chinees voor mij. Kan je eventueel de uitleg die je gegeven hebt uitleggen aan de hand van de waardes uit mijn sketch?
Soms als ik de sketch aanpas dan krijg ik het wel klaar dat de waarde van de sensor op de juiste pagina wordt weergegeven maar dan wordt de waarde niet geupdate.

Ben

Volgende

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: ehaloumave en 96 gasten