Mérleg építése. SW-t igénylő kód Arduinohoz.
SW kód
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define DT A0
#define SCK A1
#define sw 7
// Initialize the ST7735 TFT display
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
long sample=0;
float val=0;
long count=0;
unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}
void setup()
{
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
// Initialize the ST7735 TFT display
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.setTextWrap(false);
tft.setRotation(1);
Serial.begin(9600);
Serial.println(" Megmerem :) ");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(10, 10);
tft.println(" Megmerem :) ");
tft.setCursor(10, 40);
Serial.println(" Meres indul ");
tft.println(" Meres ");
delay(1000);
calibrate();
}
void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
Serial.println("Eredmeny: ");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.println("Eredmeny:");
tft.setTextSize(3);
tft.setCursor(10, 60);
Serial.println(w);
tft.print(w);
Serial.println("g ");
tft.println("g ");
tft.setTextSize(2);
delay(1000);
}
void calibrate()
{
Serial.println("Kalibracio");
Serial.println("Varj...");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.println("Kalibracio");
tft.println("Varj....");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
}
sample/=100;
Serial.println("100g & varj");
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.println("100g & varj");
count=0;
while(count<10)
{
count=readCount();
count=sample-count;
}
Serial.println("Varj...");
tft.println("Varj...");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
}
val=val/100.0;
val=val/100.0;
}