See Below for problem...
Sorry... See below
HELP... HERES CODE, I want to display iMotCounter on my ST7735 display wondering what I need to change
#include <TFT.h>
#include <SPI.h>
// pins for UNO
#define cs 10
#define dc 9
#define rst 8
#define AccelX A1 // Reading Acceleration X-Axis at analog1
#define AccelY A2 // Reading Acceleration Y-Axis at analog2
#define AccelZ A3 // Reading Acceleration Z-Axis at analog3
#define LED 13 //Onboard-LED
#define SERIAL_BAUD 9600 //for debugging
static bool bMustCalibtrate = true;
static bool flag = 0;
float threshhold = 20.0; //Low threshold, high sensitivity
TFT TFTscreen = TFT(cs, dc, rst);
char stepcountPrint [4];
static volatile int iMotCounter = 0;
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(SERIAL_BAUD);
TFTscreen.begin();
TFTscreen.background(0, 0, 0);
TFTscreen.setTextSize(2);
TFTscreen.text(" Steps = :\n" , 0, 0);
TFTscreen.setTextSize(5);
}
void loop() {
ReadMotion();
delay(100);
}
/// -----------------------------------------------
/// My Motion-Detection with first Run Calibration
/// -----------------------------------------------
void ReadMotion()
{
// Variables for the acceleromter
static float xval[8] = {0};
static float yval[8] = {0};
static float zval[8] = {0};
static float xavg;
static float yavg;
static float zavg;
float totvect[8] = {0};
float totave[8] = {0};
float xaccl[8] = {0};
float yaccl[8] = {0};
float zaccl[8] = {0};
// First run: calibration
if (bMustCalibtrate == true)
{
bMustCalibtrate = false;
digitalWrite(LED, HIGH);
float sum = 0;
float sum1 = 0;
float sum2 = 0;
for (int i = 0; i < 8; i++)
{
xval[i] = float(analogRead(AccelX));
sum = xval[i] + sum;
}
delay(100);
xavg = sum / 8.0;
//Serial.println(xavg);
for (int j = 0; j < 8; j++)
{
xval[j] = float(analogRead(AccelY));
sum1 = xval[j] + sum1;
}
yavg = sum1 / 8.0;
//Serial.println(yavg);
delay(100);
for (int i = 0; i < 8; i++)
{
zval[i] = float(analogRead(AccelZ));
sum2 = zval[i] + sum2;
}
zavg = sum2 / 8.0;
delay(100);
//Serial.println(zavg);
digitalWrite(LED, LOW);
}
// End of Calibration //
for (int i = 0; i < 8; i++)
{
xaccl[i] = float(analogRead(AccelX));
delay(1);
yaccl[i] = float(analogRead(AccelY));
delay(1);
zaccl[i] = float(analogRead(AccelZ));
delay(1);
totvect[i] = sqrt(((xaccl[i] - xavg) * (xaccl[i] - xavg)) + ((yaccl[i] - yavg) * (yaccl[i] - yavg)) + ((zval[i] - zavg) * (zval[i] - zavg)));
totave[i] = (totvect[i] + totvect[i - 1]) / 2 ;
Serial.println(totave[i]);
delay (200);
// LED Flashes at Motion
if (totave[i] > threshhold && flag == 0)
{
iMotCounter = iMotCounter + 1;
FlashLed(LED, 1, 50);
flag = 1;
}
else if (totave[i] > threshhold && flag == 1)
{
//do nothing
}
if (totave[i] < threshhold && flag == 1)
{
flag = 0;
}
Serial.println('\n');
Serial.print("iMotCounter=");
Serial.println(iMotCounter);
}
}
/// --------------------------------
/// Flashing LED´s
/// --------------------------------
void FlashLed(byte LedColor, int count, int iDelay)
{
for (int x = 0; x < count; x++)
{
digitalWrite(LedColor, HIGH);
delay(iDelay);
digitalWrite(LedColor, LOW);
delay(300);
}
}/code]
You didn't say exactly where in your code you wanted to print 'iMotCounter' to the display, but this should hopefully do what you want:-
char buffer[8]; // *Make this 14 bytes if using a Due.
itoa(iMotCounter, buffer, 10);
TFTscreen.text(buffer, 0, 0); // Prints in position x=0, y=0
It's untested - I don't have a TFT screen and have never used this library, but I think it should work.
My guess is that you want it just below this line, but you can easily add it:-
Serial.println(iMotCounter);