Les 17 (grafisch LCD 2)

Alleen lezen

Les 17 (grafisch LCD 2)

Berichtdoor pe1br » ma nov 20, 2017 1:40 am

Les17-1:
Zorg er voor dat je alle voedingsspanningen los koppelt (usb niet aangesloten op de pc en geen power adapter aangesloten op de voedingsplug).
sluit de Arduino aan volgens onderstaand schema:


Start het Arduino programma en sluit de USB kabel aan.

Typ de volgende code over of kopieer deze naar de arduino ide:
Code: Selecteer alles
//TFT
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define _cs 10
#define _dc 9
#define _rst 7

// INPUTS
#define temppin A0
#define potpin A1
#define ldrpin A2

// ILI9341:
Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _rst);
int black = ILI9341_BLACK;
int white = ILI9341_WHITE;
int blue = ILI9341_BLUE;
int green = ILI9341_GREEN;
int red = ILI9341_RED;
int orange = ILI9341_YELLOW;

//readvalues
int measure[] = {0, temppin, potpin, ldrpin};
int minvalue[] = {0, 0, 0, 0, 0};
int maxvalue[] = {0, 255, 255, 255, 255};

//startreadvalues 
String barnames[] = {"", "  TEMP", "  POT", "  LDR+"};

//verticalbartext
int oldvalue[] = {0, 0, 0, 0};
int barpos[] = {260, 36, 116, 196};

//code
void cleartext(String text, int x, int y, int textsize) {
  tft.fillRect(x, y, textsize * 6 * text.length() , textsize * 7, black);
}

void writetext(String text, int x, int y, int textsize, int textcolor) {
  tft.setCursor(x, y);
  tft.setTextColor(textcolor);
  tft.setTextSize(textsize);
  tft.print(text);
}


void verticalbartext(String text, int x) {
  writetext(text, x - 36 ,170, 2, white);
  writetext("150%", x - 36, 7, 1, white);
  writetext("125%", x - 36, 32, 1, white);
  writetext("100%", x - 36, 57, 1, white);
  writetext("75%", x - 30, 82, 1, white);
  writetext("50%", x - 30, 107, 1, white);
  writetext("25%", x - 30, 132, 1, white);
  writetext("0%", x - 24, 157, 1, white);
  tft.drawFastVLine(x - 7, 10, 150, white);
  tft.drawFastHLine(x - 11, 10, 9, white);
  tft.drawFastHLine(x - 11, 35, 9, white);
  tft.drawFastHLine(x - 11, 60, 9, white);
  tft.drawFastHLine(x - 11, 85, 9, white);
  tft.drawFastHLine(x - 11, 110, 9, white);
  tft.drawFastHLine(x - 11, 135, 9, white);
  tft.drawFastHLine(x - 11, 160, 9, white);
  tft.drawFastHLine(x, 160, 35, green);
}

void readvalues() {
  for (int y = 1; y < 4; y++) {
    verticalbar(y, barpos[y], map((analogRead(measure[y]) / 4), minvalue[y], maxvalue[y], 0, 150));
  }
}

void verticalbar(int number, int x, int value) {
  if (value < oldvalue[number])
    for (int y = oldvalue[number]; y >= value; y--) {
      if (y >= 1 && y <= 160) tft.drawFastHLine(x,160 - y, 35, black);
    }
  if (value > oldvalue[number]) {
    for (int y = oldvalue[number]; y <= value; y++) {
      if (y >= 0 && y <= 150) {
        if (y < 100) tft.drawFastHLine(x, 160 - y, 35, green);
        if(y >= 100 && y <= 110) tft.drawFastHLine(x, 160 - y, 35, orange);
        if (y >= 111) tft.drawFastHLine(x, 160 - y, 35, red);
      }
    }
  }
  oldvalue[number] = value;
}

void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.fillScreen(black);
  tft.setRotation(3); //0-3
  verticalbartext(barnames[1], barpos[1]);
  verticalbartext(barnames[2], barpos[2]);
  verticalbartext(barnames[3], barpos[3]);
}

void loop() {
  readvalues();
}


Beweeg je hand over de LCD, draai aan de potmeter en verwarm de temperatuur sensor.
Bijlagen
les17-1.jpg
(178.48 KiB) Nog niet gedownload
pe1br
 
Berichten: 53
Geregistreerd: do apr 14, 2016 3:37 am

Keer terug naar Lessen

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron