INTP:
There is no such thing as reading continuously. And with your use of delay, there is no hope of getting anything useful with any of it so far.
Your code literally goes
-read A0
-print stuff for about half a second (all 23 lines) while NOT updating A0
-read A0 again
-print stuff for about half a second (all 23 lines again)
etc.
What kind of frequency are you dealing with? Do you know if the Arduino is fast enough to detect? What do you mean crossover (0V)? Are you expecting negative voltage?
To start, you need to be reading every single loop. As it stands, a single instance of <=0.05 triggers a meaningless print of 23 values only separated by your 10ms delay and however long it takes to print. That data in no way represents any phases.
Thank you for your explanation, that was helpful. I mistakenly said crossover voltage. Yes i've checked (as well as others) that the arduino is capable of handling this project. I see what you mean about not updating the voltage every time through the loop. Here's my updated code. Thoughts? I still dont seem to be getting an accurate timing for my outputs.
#include <Adafruit_FT6206.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
boolean CrossOverPt = false;
#define FRAME_X 60 // X coordinate for the "Press to Begin" button
#define FRAME_Y 110 // Y coordinate for the "Press to Begin" button
#define FRAME_W 200 // Width for the "Press to Begin" button
#define FRAME_H 50 // Height for the "Press to Begin" button
#define FRAME1_X 20 // X coordinate for the "??????" banner
#define FRAME1_Y 5 // Y coordinate for the "???????" banner
#define FRAME1_W 280 // Width for the "????????" banner
#define FRAME1_H 50 // Height for the "????????" banner
int SetupDelay = 3000; //Delay time (3s) for Setup Screen (??????? - Hall Sensor Alignment Test) to Display.
// Setup()
//****************************************************************************************************************************************************************************
void setup()
{
Serial.begin(9600); // Starting the serial monitor/plotter - Baud rate = 9600
tft.begin(); // Start use of Adafruit TFT 2.8" Capacitive Touchscreen
if (!ctp.begin(40)) {
Serial.println("Unable to start touchscreen."); // If the Touchscreen is not connected, the previous message will display on the serial monitor
}
else {
Serial.println("Touchscreen started."); // If the Touchscreen is connected, the previous message will display on the serial monitor
}
tft.fillScreen(ILI9341_BLUE); // Fills the entire screen blue
// origin = left,top landscape (USB left upper)
tft.setRotation(1); // Rotate the screen to use horizontally
drawFrame(); // Calls the drawFrame function - Fills in the buttons and outlines for the setup screen
addText(); // Calls the addText function - Adds all text to the setup screen
//delay(SetupDelay); // Delays the next action for 3s
tft.fillScreen(ILI9341_BLACK); // Fills the entire screen blue
analog0();
}// End of Setup()
//****************************************************************************************************************************************************************************
// Loop()
//****************************************************************************************************************************************************************************
void loop()
{
}
// End of Loop()
//****************************************************************************************************************************************************************************
// Functions()
//************************************************************************************************************************************************************************************************************
void drawFrame()
{
tft.fillRect(FRAME1_X, FRAME1_Y, FRAME1_W, FRAME1_H, ILI9341_WHITE);
tft.drawRect(FRAME1_X, FRAME1_Y, FRAME1_W, FRAME1_H, ILI9341_BLACK);
tft.drawLine(0, 75, 320, 75, ILI9341_WHITE);
tft.drawLine(0, 58, 320, 58, ILI9341_WHITE);
tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_WHITE);
tft.drawRect(0, 0, 320, 240, ILI9341_BLACK);
}
void addText()
{
tft.setCursor(45, 20);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(3);
tft.print("???????");
tft.setCursor(5, 60);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Hall Sensor Alignment Test");
tft.setCursor(75, 125);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Press to Begin");
tft.setCursor(180, 230);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.print("DEVELOPED JULY 14, 2017");
}
void analog0()
{
//Read input from analog shield Channel A0 (Phase A) and return the time it crosses over 0V.
// Timing for phase A crossovers
unsigned long T_PhaseAcross[23]; // Array holding the intervals for crossover (0V) times.
/* for (int n = 1; n <= 1; n++) { // For loop to ensure that this sequence is only initiated once (only fill in the values of the array for one full cycle)
//Serial.print("n = "); //Used for debugging purposes only
//Serial.println(n);*/
float VoltA0 = (analogRead(0)) / 203.8; //Modified (203.8 w/out screen - 210 with screen?? KB 07/19/17) from 204.8 for resolution tuning;
if (VoltA0 <= 0.05) { // Ensures proper timing that first reading will fall directly on a falling/rising edge when Voltage is 0.
delay(10);
Serial.println("Delayed");
}
int i = 0;
while (i <= 24) { // While loop to fill in the Array with the 25 interval values
VoltA0 = (analogRead(0)) / 203.8; //Modified (203.8 w/out screen - 210 with screen?? KB 07/19/17) from 204.8 for resolution tuning;
unsigned long currentMillis = millis(); // Starts a timer in milleseconds
unsigned long previousMillis;
//Serial.print("i = "); //Used for debugging purposes only
// Serial.println(i);
if (VoltA0 <= 0.05) {
CrossOverPt = true;
T_PhaseAcross[i] = currentMillis - previousMillis; // Interval timing to get 24 crossover points
previousMillis = currentMillis;
Serial.print("T_PhaseAcross"); //Used for debugging purposes only
Serial.print(i);
Serial.print(" = ");
Serial.println(T_PhaseAcross[i]);
i++;
}
}
int avg = (T_PhaseAcross[4] + T_PhaseAcross[6] + T_PhaseAcross[8] + T_PhaseAcross[10] + T_PhaseAcross[12] + T_PhaseAcross[14] + T_PhaseAcross[16] + T_PhaseAcross[18] + T_PhaseAcross[20] + T_PhaseAcross[22] + T_PhaseAcross[24]) / 11;
Serial.print("Average = ");
Serial.print(avg);
Serial.println(" ms");
}
}
// End of Functions()
//****************************************************************************************************************************************************************************