Hello all,
I am very new to the Arduino world and I am trying to write the code for a Hall Sensor to Motor phase alignment tester/display. I am using the UNO R3. My code attempts to monitor voltages (A0 only for now) and record a time ( or intervals) for every time the voltage level drops to 0V for 24 times. Once I have these intervals, I then take the average and compare to one of the other channels.
For example, if motor phase A is channel A0 and Hall Sensor 1 is channel A1, i would complete the above process and compare the averaged time interval which should tell me how far off the Hall sensor alignment is off from the motor phase alignment (the conversion can be made to electrical degrees and expressed this way). My code is shown below:
#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()
//****************************************************************************************************************************************************************************
My problem seems to be that the time intervals that are recorded and expressed aren't right which leads me to believe i have written something wrong. The intervals for the motor that i am currently using should average around 9 ms. Does anyone see any blatantly wrong errors in my coding?
