Flash radiotherapy project (aka cancer treatment)

Hi All,

I need a bit of help with this code, at the moment I am trying to write a program to count the number of pulses from a Linac (one of these [Radiation Therapy | Products | Linear Accelerators | Elekta](https://Elekta Linac)) and when it has outputted the right amount of pulses it will trigger and stop the Linac. The code is not yet complete, however it does operate. There are two problems I need help with,

  1. when I select the pulse counter ( void setPulsetriggervalue() )
    and move the slider left to right it works fine, but when you go right to left the number count seems to go wrong when you get to the left hand side again.

  2. when using the oscilloscope function ( void Oscilloscope() ), it will display a wave form ok, but when you exit the screen using the back to main menu button , it goes to the main menu again and overwrites the main menu with what appears to be the last remains of the oscilloscope signal.

I am using a ILI9341 screen with a Arduino Mega and a mega shield.

I have yet to install a sub menu on to the pulse counter option but will do this after sorting out these problems

[table][tr][td][size=14pt][sub]Code:[/sub][/size][hr][/td][/tr][tr][td][size=9pt][tt]#include <UTFT.h>
#include <URTouch.h>

//==== Creating Objects
UTFT myGLCD(ILI9341_16, 38, 39, 40, 41);
URTouch myTouch( 6, 5, 4, 3, 2);

//==== Defining Variables
char buf[24];
int x, y;
int Input = 0;
byte Sample[320];
byte OldSample[320];
int StartSample = 0;
int EndSample = 0;
int Max = 100;
int Min = 100;
int dTime = .25;
int SampleSize = 0;
int SampleTime = 0;
int dgvh;
int hpos = 50; //set 0v on horizontal grid
int vsens = 4; // vertical sensitivity
bool scope = false;

// Define various ADC prescaler
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

char currentPage;

long duration;

int xTrigger = 38;

void setupcolor=#000000[/color] {
// Initial setup
myGLCD.InitLCDcolor=#000000[/color];
myGLCD.clrScrcolor=#000000[/color];
myTouch.InitTouchcolor=#000000[/color];
myTouch.setPrecisioncolor=#000000[/color];
drawHomeScreencolor=#000000[/color]; // Draws the Home Screen

// Defining Pin Modes

currentPage = '0'; // Indicates that we are at Home Screen

pinMode(0, INPUT);
// set up the ADC
ADCSRA &= ~PS_128; // remove bits set by Arduino library

// you can choose a prescaler from below.
// PS_16, PS_32, PS_64 or PS_128
ADCSRA |= PS_64; // set our own prescaler
}

// ====== Custom Funtions ======

// drawHomeScreen - Custom Function
void drawHomeScreencolor=#000000[/color] {
// Title
scope = false;
myGLCD.setBackColor(0, 0, 0); // Sets the background color of the area where the text will be printed to black
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFontcolor=#000000[/color]; // Sets font to big
myGLCD.print("Flash Radiotherapy", CENTER, 10); // Prints the string on the screen
myGLCD.print("Utility", CENTER, 30);
myGLCD.setColor(255, 0, 0); // Sets color to red
myGLCD.drawLine(0, 55, 319, 55); // Draws the red line
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFontcolor=#000000[/color]; // Sets the font to small
myGLCD.print("By Steve Horton", CENTER, 60);
myGLCD.setFontcolor=#000000[/color];
myGLCD.print("Select Option", CENTER, 80);

// Button - Oscilloscope
myGLCD.setColor(16, 167, 103); // Sets green color
myGLCD.fillRoundRect (20, 110, 300, 155); // Draws filled rounded rectangle
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.drawRoundRect (20, 110, 300, 155); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
myGLCD.setFontcolor=#000000[/color]; // Sets the font to big
myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
myGLCD.print("Oscilloscope", CENTER, 125); // Prints the string
// Button - Pulse counter
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (20, 175, 300, 220);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (20, 175, 300, 220);
myGLCD.setFontcolor=#000000[/color];
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("Pulse Counter", CENTER, 190);
}

void DrawGridcolor=#000000[/color]
{ myGLCD.setColor( 0, 200, 0);
for ( dgvh = 0; dgvh < 5; dgvh ++) {
myGLCD.drawLine( dgvh * 50, 0, dgvh * 50, 240);
myGLCD.drawLine( 0, dgvh * 50, 245 , dgvh * 50);
}
myGLCD.drawLine( 245, 0, 245, 240);
myGLCD.drawLine( 0, 239, 245, 239);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (250, 1, 310, 50);
}
//-------touchscreen position sub
void touchcolor=#000000[/color] {
while color=#000000[/color]
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (250, 1, 310, 50);
myTouch.readcolor=#000000[/color];
x = myTouch.getXcolor=#000000[/color];
y = myTouch.getYcolor=#000000[/color];
if ((y >= 1) && (y <= 50) ) //Return to home screen button
{ if ((x >= 250) && (x <= 300) && scope == true)
{ scope = false;
currentPage = '0';
myGLCD.clrScrcolor=#000000[/color];
drawHomeScreencolor=#000000[/color];
}

[color=#000000]}[/color]

}

}

// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while color=#000000[/color]
myTouch.readcolor=#000000[/color];
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
void buttonscolor=#000000[/color] {
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (250, 1, 310, 50);
}
//====================================================
void Oscilloscopecolor=#000000[/color] {
buttonscolor=#000000[/color];
while (scope == true) {
DrawGridcolor=#000000[/color];
touchcolor=#000000[/color];

[color=#434f54]// Collect the analog data into an array[/color]

[color=#000000]StartSample[/color] [color=#434f54]=[/color] [color=#d35400]micros[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color] [color=#00979c]int[/color] [color=#000000]xpos[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color]
      [color=#000000]xpos[/color] [color=#434f54]<[/color] [color=#000000]240[/color][color=#000000];[/color] [color=#000000]xpos[/color] [color=#434f54]++[/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#d35400]analogRead[/color][color=#000000]([/color][color=#000000]A0[/color][color=#000000])[/color] [color=#434f54]*[/color] [color=#000000]5[/color] [color=#434f54]/[/color] [color=#000000]102[/color][color=#000000];[/color]
  [color=#d35400]delayMicroseconds[/color][color=#000000]([/color][color=#000000]dTime[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
[color=#000000]EndSample[/color] [color=#434f54]=[/color] [color=#d35400]micros[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#434f54]// Display the collected analog data from array[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color] [color=#00979c]int[/color] [color=#000000]xpos[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]xpos[/color] [color=#434f54]<[/color] [color=#000000]239[/color][color=#000000];[/color]
      [color=#000000]xpos[/color] [color=#434f54]++[/color][color=#000000])[/color]
[color=#000000]{[/color]
  [color=#434f54]// Erase previous display[/color]
  [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]setColor[/color][color=#000000]([/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]drawLine[/color] [color=#000000]([/color][color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]255[/color] [color=#434f54]-[/color] [color=#000000]OldSample[/color][color=#000000][[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#000000]][/color][color=#434f54]*[/color] [color=#000000]vsens[/color] [color=#434f54]-[/color] [color=#000000]hpos[/color][color=#434f54],[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]255[/color] [color=#434f54]-[/color] [color=#000000]OldSample[/color][color=#000000][[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]2[/color][color=#000000]][/color][color=#434f54]*[/color] [color=#000000]vsens[/color] [color=#434f54]-[/color] [color=#000000]hpos[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]xpos[/color] [color=#434f54]==[/color] [color=#000000]0[/color][color=#000000])[/color] [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]drawLine[/color] [color=#000000]([/color][color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]239[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#434f54]// Draw the new data[/color]
  [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]setColor[/color] [color=#000000]([/color][color=#000000]255[/color][color=#434f54],[/color] [color=#000000]255[/color][color=#434f54],[/color] [color=#000000]255[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]drawLine[/color] [color=#000000]([/color][color=#000000]xpos[/color][color=#434f54],[/color] [color=#000000]255[/color] [color=#434f54]-[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color][color=#434f54]*[/color] [color=#000000]vsens[/color] [color=#434f54]-[/color] [color=#000000]hpos[/color][color=#434f54],[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]255[/color] [color=#434f54]-[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#000000]][/color][color=#434f54]*[/color] [color=#000000]vsens[/color] [color=#434f54]-[/color] [color=#000000]hpos[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
[color=#434f54]// Determine sample voltage peak to peak[/color]
[color=#000000]Max[/color] [color=#434f54]=[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]100[/color][color=#000000]][/color][color=#000000];[/color]
[color=#000000]Min[/color] [color=#434f54]=[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]100[/color][color=#000000]][/color][color=#000000];[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color] [color=#00979c]int[/color] [color=#000000]xpos[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color]
      [color=#000000]xpos[/color] [color=#434f54]<[/color] [color=#000000]240[/color][color=#000000];[/color] [color=#000000]xpos[/color] [color=#434f54]++[/color][color=#000000])[/color]
[color=#000000]{[/color]
  [color=#000000]OldSample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color][color=#000000];[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color] [color=#434f54]>[/color] [color=#000000]Max[/color][color=#000000])[/color] [color=#000000]Max[/color] [color=#434f54]=[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color][color=#000000];[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color] [color=#434f54]<[/color] [color=#000000]Min[/color][color=#000000])[/color] [color=#000000]Min[/color] [color=#434f54]=[/color] [color=#000000]Sample[/color][color=#000000][[/color] [color=#000000]xpos[/color][color=#000000]][/color][color=#000000];[/color]
[color=#000000]}[/color]
[color=#434f54]// display the sample time, delay time and trigger level[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]setBackColor[/color][color=#000000]([/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]255[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]setFont[/color][color=#000000]([/color] [color=#000000]SmallFont[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]setBackColor[/color][color=#000000]([/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]255[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"Return"[/color][color=#434f54],[/color] [color=#000000]260[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"to Main"[/color][color=#434f54],[/color] [color=#000000]252[/color][color=#434f54],[/color] [color=#000000]20[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"Menu"[/color][color=#434f54],[/color] [color=#000000]265[/color][color=#434f54],[/color] [color=#000000]35[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]SampleTime[/color] [color=#434f54]=[/color] [color=#000000]([/color] [color=#000000]EndSample[/color] [color=#434f54]-[/color] [color=#000000]StartSample[/color][color=#000000])[/color] [color=#434f54]/[/color] [color=#000000]1000[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"Sec."[/color][color=#434f54],[/color] [color=#000000]205[/color][color=#434f54],[/color] [color=#000000]210[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]" "[/color][color=#434f54],[/color] [color=#000000]280[/color][color=#434f54],[/color] [color=#000000]30[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#000000]itoa[/color][color=#000000]([/color] [color=#000000]SampleTime[/color][color=#434f54],[/color] [color=#000000]buf[/color][color=#434f54],[/color] [color=#000000]10[/color][color=#000000])[/color][color=#434f54],[/color] [color=#000000]205[/color][color=#434f54],[/color] [color=#000000]220[/color][color=#000000])[/color][color=#000000];[/color]
[color=#434f54]// Range of 0 to 64 * 78 = 4992 mV[/color]
[color=#000000]SampleSize[/color] [color=#434f54]=[/color] [color=#000000]([/color] [color=#000000]Max[/color] [color=#434f54]-[/color] [color=#000000]Min[/color][color=#000000])[/color] [color=#434f54]*[/color] [color=#000000]78[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"mVolt"[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#434f54],[/color] [color=#000000]210[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color] [color=#000000]itoa[/color][color=#000000]([/color] [color=#000000]SampleSize[/color][color=#434f54],[/color] [color=#000000]buf[/color][color=#434f54],[/color] [color=#000000]10[/color][color=#000000])[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#434f54],[/color] [color=#000000]220[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#000000]itoa[/color][color=#000000]([/color] [color=#d35400]analogRead[/color][color=#000000]([/color][color=#000000]A0[/color][color=#000000])[/color] [color=#434f54]*[/color] [color=#000000]4.15[/color] [color=#434f54]/[/color] [color=#000000]10.23[/color][color=#434f54],[/color] [color=#000000]buf[/color][color=#434f54],[/color] [color=#000000]10[/color][color=#000000])[/color][color=#434f54],[/color] [color=#000000]110[/color] [color=#434f54],[/color] [color=#000000]220[/color][color=#000000])[/color][color=#000000];[/color]

}

}
//====================================================
void pulsetriggerControlcolor=#000000[/color] {//Draw pulse trigger Control screen
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFontcolor=#000000[/color];
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFontcolor=#000000[/color];
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFontcolor=#000000[/color];
myGLCD.print("Trigger pulse", CENTER, 45);
myGLCD.print("Control", CENTER, 65);
myGLCD.print("Select Trigger", CENTER, 95);
myGLCD.print("point", CENTER, 110);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(0, 87, 319, 87);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRect(30, 138, 310, 148); // Pulse trigger Slider

}

//====================================================
//============= se// Maps the values of the X - Axis from 38 to 0 and 310 to 1024, because we need values from 0 to 255 for turning on the ledtPulsetriggervalue() - Custom Funtion
void setPulsetriggervaluecolor=#000000[/color] {
if color=#000000[/color] {
myTouch.readcolor=#000000[/color];
x = myTouch.getXcolor=#000000[/color];
y = myTouch.getYcolor=#000000[/color];
// Area of the xTrigger slider
if ( (y >= 130) && (y <= 156)) {
xTrigger = x; // Stores the X value where the screen has been pressed in to variable xR
if (xTrigger <= 38) { // Confines the area of the slider to be above 38 pixels
xTrigger = 38;
}
if (xTrigger >= 303) { /// Confines the area of the slider to be under 310 pixels
xTrigger = 303;
}
}

}
// Maps the values of the X - Axis from 38 to 0 and 310 to 1024, because we need
//values from 0 to 1024 for setting the value at which we trigger the PRF check
int long xTriggercount = map(xTrigger, 38, 310, 0, 1024);
myGLCD.setColor(255, 255, 255);
myGLCD.setFontcolor=#000000[/color];
myGLCD.print( itoa( xTriggercount, buf, 10), 5, 180);

// Draws the positioner for trigger pulse
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(xTrigger, 139, (xTrigger + 4), 147); // Positioner
myGLCD.setColor(xTriggercount, 0, 0);
myGLCD.fillRect(31, 139, (xTrigger - 1), 147);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((xTrigger + 5), 139, 309, 147);

}

void loopcolor=#000000[/color] {
// Home Screen
if (currentPage == '0') {
scope = false;
if color=#000000[/color] {
myTouch.readcolor=#000000[/color];
x = myTouch.getXcolor=#000000[/color]; // X coordinate where the screen has been pressed
y = myTouch.getYcolor=#000000[/color]; // Y coordinates where the screen has been pressed
// If we press the Distance Sensor Button
if ((x >= 20) && (x <= 300) && (y >= 110) && (y <= 155)) {
drawFrame(20, 110, 300, 155); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1'; // Indicates that we are the first example
myGLCD.clrScrcolor=#000000[/color]; // Clears the screen
scope = true;
Oscilloscopecolor=#000000[/color]; // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
}
// If we press the Pulse counter button
if ((x >= 20) && (x <= 300) && (y >= 175) && (y <= 220)) {
drawFrame(20, 175, 300, 220);
currentPage = '2';
myGLCD.clrScrcolor=#000000[/color];
pulsetriggerControlcolor=#000000[/color];
}

[color=#000000]}[/color]

}

// pulsetriggerControl
if (currentPage == '2') {
setPulsetriggervaluecolor=#000000[/color];
if color=#000000[/color] {
myTouch.readcolor=#000000[/color];
x = myTouch.getXcolor=#000000[/color];
y = myTouch.getYcolor=#000000[/color];

  [color=#434f54]//Back button[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]([/color][color=#000000]x[/color] [color=#434f54]>=[/color] [color=#000000]10[/color][color=#000000])[/color] [color=#434f54]&&[/color] [color=#000000]([/color][color=#000000]x[/color] [color=#434f54]<=[/color] [color=#000000]60[/color][color=#000000])[/color] [color=#434f54]&&[/color] [color=#000000]([/color][color=#000000]y[/color] [color=#434f54]>=[/color] [color=#000000]10[/color][color=#000000])[/color] [color=#434f54]&&[/color] [color=#000000]([/color][color=#000000]y[/color] [color=#434f54]<=[/color] [color=#000000]36[/color][color=#000000])[/color][color=#000000])[/color] [color=#000000]{[/color]
    [color=#000000]drawFrame[/color][color=#000000]([/color][color=#000000]10[/color][color=#434f54],[/color] [color=#000000]10[/color][color=#434f54],[/color] [color=#000000]60[/color][color=#434f54],[/color] [color=#000000]36[/color][color=#000000])[/color][color=#000000];[/color]
    [color=#000000]currentPage[/color] [color=#434f54]=[/color] [color=#00979c]'0'[/color][color=#000000];[/color]
    [color=#000000]myGLCD[/color][color=#434f54].[/color][color=#d35400]clrScr[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
    [color=#000000]drawHomeScreen[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]

  [color=#000000]}[/color]
[color=#000000]}[/color]

}
}[/tt][/size][/td][/tr][/table]

Any help would be greatly appreciated

Steve
[Mod edit]
@Steve_H , I appreciate that you might well have the appropriate skills to do this safely. For the benefit of anyone else reading this please be aware that there are dangers involved in this project and you should not attempt to do what Steve is doing unless you have the approprate skills to understand the risks and how to mitigate them.
Thank you.

Hi Steve,

I really don't want to be "that guy", but "Arduino" and "Controlling things that emit radiation" seem like there could well be something potentially dangerous there.

Hi CJSouthern,

I understand your concern, I work as a clinical technologist within the NHS, I am trained to use these Linacs and do follow strict rules when using them. You cannot go in the room while they they emitting Xrays . Basically overgrown xray machines.

I don't want to be "that guy" either, but I find it impossible to believe that the safety of humans is being placed in the hands of someone with an Arduino and the need to ask for help with debugging. It seems inconceivable to me, unless I have misunderstood.

Are you authorised to do this work? If this work needs doing, why hasn't the NHS asked for Elekta to do the work and get it tested and approved in the normal way?

1 Like

Hi Steve,

Thanks for the follow-up. I'm sure that you are ... and I'm sure that you do ... but there's just something about "Arduino" and "it will trigger and stop the Linac." used in the same sentence that scares the "heck" out of me.

I'm seeing huge red flags here.

Hi Steve Thackery,

YES You have misunderstood and to be honest I have found you reply to be insulting that I am doing unauthorized work that would endanger peoples lives. This is research work that no one has done before. My skill set used primarily in ensuring that these machines are safe for clinical use and unfortunately not as a programmer. All this device is meant to do is count pulses and display a waveform using a oscilloscope program and will not in any way do anything to the machine. I posted this on here in order to find help and it looks as though I am not going to get any

Don't be insulted - it's a totally legitimate concern and question.

You said it will turn it off, which implies that a bug in your code might result in it not turning off, which makes all of us wonder if there is a risk of overdosing a patient because it didn't switch off. It's reasonable to wonder that.

Stop sulking and recognise that reasonable people will want to express concerns over anything to do with medical equipment. I repeat: it is totally reasonable for us to do that.

Now that you've answered our questions, wait a little longer and people will surely be along who can help. Have patience.

1 Like

Hi, @Steve_H
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

It will show you how to post your code so it is in a scrolling window.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Could you please post the code in a more readable way? I tried going through it, but I got kind of lost in the combination of C++ and HTML that resulted from a failed copy-paste exercise.

will do, thanks Tom

#include <UTFT.h>
#include <URTouch.h>

//==== Creating Objects
UTFT myGLCD(ILI9341_16, 38, 39, 40, 41);
URTouch  myTouch( 6, 5, 4, 3, 2);

//==== Defining Variables
char buf[24];
int x, y;
int Input = 0;
byte Sample[320];
byte OldSample[320];
int StartSample = 0;
int EndSample = 0;
int Max = 100;
int Min = 100;
int dTime = .25;
int SampleSize = 0;
int SampleTime = 0;
int dgvh;
int hpos = 50; //set 0v on horizontal grid
int vsens = 4; // vertical sensitivity
bool scope = false;

// Define various ADC prescaler
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

char currentPage;

long duration;

int xTrigger = 38;



void setup() {
  // Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  drawHomeScreen();  // Draws the Home Screen

  // Defining Pin Modes


  currentPage = '0'; // Indicates that we are at Home Screen

  pinMode(0, INPUT);
  // set up the ADC
  ADCSRA &= ~PS_128; // remove bits set by Arduino library

  // you can choose a prescaler from below.
  // PS_16, PS_32, PS_64 or PS_128
  ADCSRA |= PS_64; // set our own prescaler
}



// ====== Custom Funtions ======

// drawHomeScreen - Custom Function
void drawHomeScreen() {
  // Title
  scope = false;
  myGLCD.clrScr();
  myGLCD.setBackColor(0, 0, 0); // Sets the background color of the area where the text will be printed to black
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.setFont(BigFont); // Sets font to big
  myGLCD.print("Flash Radiotherapy", CENTER, 10); // Prints the string on the screen
  myGLCD.print("Utility", CENTER, 30);
  myGLCD.setColor(255, 0, 0); // Sets color to red
  myGLCD.drawLine(0, 55, 319, 55); // Draws the red line
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.setFont(SmallFont); // Sets the font to small
  myGLCD.print("By Steve Horton", CENTER, 60);
  myGLCD.setFont(BigFont);
  myGLCD.print("Select Option", CENTER, 80);

  // Button - Oscilloscope
  myGLCD.setColor(16, 167, 103); // Sets green color
  myGLCD.fillRoundRect (20, 110, 300, 155); // Draws filled rounded rectangle
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.drawRoundRect (20, 110, 300, 155); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
  myGLCD.setFont(BigFont); // Sets the font to big
  myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
  myGLCD.print("Oscilloscope", CENTER, 125); // Prints the string
  // Button - Pulse counter
  myGLCD.setColor(16, 167, 103);
  myGLCD.fillRoundRect (20, 175, 300, 220);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (20, 175, 300, 220);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(16, 167, 103);
  myGLCD.print("Pulse Counter", CENTER, 190);
}

void DrawGrid()
{ myGLCD.setColor( 0, 200, 0);
  for ( dgvh = 0; dgvh < 5; dgvh ++) {
    myGLCD.drawLine( dgvh * 50, 0, dgvh * 50, 240);
    myGLCD.drawLine( 0, dgvh * 50, 245 , dgvh * 50);
  }
  myGLCD.drawLine( 245, 0, 245, 240);
  myGLCD.drawLine( 0, 239, 245, 239);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (250, 1, 310, 50);
}
//-------touchscreen position sub
void touch() {
  while (myTouch.dataAvailable())
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.drawRoundRect (250, 1, 310, 50);
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((y >= 1) && (y <= 50) ) //Return to home screen button
    { if ((x >= 250) && (x <= 300) && scope == true)
      { scope = false;
        currentPage = '0';
        myGLCD.clrScr();
        drawHomeScreen();
      }

    }
  }

}



// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}


//====================================================
void Oscilloscope() {
 
 myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (250, 1, 310, 50);
  while (scope == true) {
    DrawGrid();
    touch();

    // Collect the analog data into an array

    StartSample = micros();
    for ( int xpos = 0;
          xpos < 240; xpos ++) {
      Sample[ xpos] = analogRead(A0) * 5 / 102;
      delayMicroseconds(dTime);
    }
    EndSample = micros();
    // Display the collected analog data from array
    for ( int xpos = 0; xpos < 239;
          xpos ++)
    {
      // Erase previous display
      myGLCD.setColor( 0, 0, 0);
      myGLCD.drawLine (xpos + 1, 255 - OldSample[ xpos + 1]* vsens - hpos, xpos + 2, 255 - OldSample[ xpos + 2]* vsens - hpos);
      if (xpos == 0) myGLCD.drawLine (xpos + 1, 1, xpos + 1, 239);
      // Draw the new data
      myGLCD.setColor (255, 255, 255);
      myGLCD.drawLine (xpos, 255 - Sample[ xpos]* vsens - hpos, xpos + 1, 255 - Sample[ xpos + 1]* vsens - hpos);
    }
    // Determine sample voltage peak to peak
    Max = Sample[ 100];
    Min = Sample[ 100];
    for ( int xpos = 0;
          xpos < 240; xpos ++)
    {
      OldSample[ xpos] = Sample[ xpos];
      if (Sample[ xpos] > Max) Max = Sample[ xpos];
      if (Sample[ xpos] < Min) Min = Sample[ xpos];
    }
    // display the sample time, delay time and trigger level
    myGLCD.setBackColor( 0, 0, 255);
    myGLCD.setFont( SmallFont);
    myGLCD.setBackColor( 0, 0, 255);
    myGLCD.print("Return", 260, 5);
    myGLCD.print("to Main", 252, 20);
    myGLCD.print("Menu", 265, 35);
    SampleTime = ( EndSample - StartSample) / 1000;
    myGLCD.print("Sec.", 205, 210);
    myGLCD.print(" ", 280, 30);
    myGLCD.print(itoa( SampleTime, buf, 10), 205, 220);
    // Range of 0 to 64 * 78 = 4992 mV
    SampleSize = ( Max - Min) * 78;
    myGLCD.print("mVolt", 5, 210);
    myGLCD.print( itoa( SampleSize, buf, 10), 5, 220);
    myGLCD.print(itoa( analogRead(A0) * 4.15 / 10.23, buf, 10), 110 , 220);
  }

}
//====================================================
void pulsetriggerControl() {//Draw pulse trigger Control screen
  myGLCD.setColor(100, 155, 203);
  myGLCD.fillRoundRect (10, 10, 60, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 10, 60, 36);
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRoundRect (180, 160, 220, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (180, 160, 220, 180);
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRoundRect (180, 190, 220, 210);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (180, 190, 220, 210);
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRoundRect (240, 160, 280, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (240, 160, 280, 180);
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRoundRect (240, 190, 280, 210);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (240, 190, 280, 210);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(100, 155, 203);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back to Main Menu", 70, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Trigger pulse", CENTER, 45);
  myGLCD.print("Control", CENTER, 65);
  myGLCD.print("Select Trigger", CENTER, 95);
  myGLCD.print("point", CENTER, 110);
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawLine(0, 87, 319, 87);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRect(30, 138, 310, 148); // Pulse trigger Slider

}

//====================================================
//============= se// Maps the values of the X - Axis from 38 to 0 and 310 to 255, because we need values from 0 to 255 for turning on the ledtPulsetriggervalue() - Custom Funtion
void setPulsetriggervalue() {
  if (myTouch.dataAvailable()) {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    // Area of the Red color slider
    if ( (y >= 130) && (y <= 156)) {
      xTrigger = x; // Stores the X value where the screen has been pressed in to variable xR
      if (xTrigger <= 38) { // Confines the area of the slider to be above 38 pixels
        xTrigger = 38;
      }
      if (xTrigger >= 303) { /// Confines the area of the slider to be under 310 pixels
        xTrigger = 303;
      }
    }


  }
  // Maps the values of the X - Axis from 38 to 0 and 310 to 1024, because we need 
  //values from 0 to 1024 for setting the value at which we trigger the PRF check
  int long xTriggercount = map(xTrigger, 38, 310, 0, 1024);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSegNumFont);
  myGLCD.print( itoa( xTriggercount, buf, 10), 5, 180);

  // Draws the positioner for trigger pulse
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(xTrigger, 139, (xTrigger + 4), 147); // Positioner
  myGLCD.setColor(xTriggercount, 0, 0);
  myGLCD.fillRect(31, 139, (xTrigger - 1), 147);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect((xTrigger + 5), 139, 309, 147);

}

void loop() {
  // Home Screen
  
  if (currentPage == '0') {
    scope = false;
    if (myTouch.dataAvailable()) {
      myTouch.read();
      x = myTouch.getX(); // X coordinate where the screen has been pressed
      y = myTouch.getY(); // Y coordinates where the screen has been pressed
      // If we press the Distance Sensor Button
      if ((x >= 20) && (x <= 300) && (y >= 110) && (y <= 155)) {
        drawFrame(20, 110, 300, 155); // Custom Function -Highlighs the buttons when it's pressed
        currentPage = '1'; // Indicates that we are the first example
        myGLCD.clrScr(); // Clears the screen
        scope = true;
        Oscilloscope(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
      }
      // If we press the Pulse counter button
      if ((x >= 20) && (x <= 300) && (y >= 175) && (y <= 220)) {
        drawFrame(20, 175, 300, 220);
        currentPage = '2';
        myGLCD.clrScr();
        pulsetriggerControl();
      }

    }
  }

  // pulsetriggerControl
  if (currentPage == '2') {
    setPulsetriggervalue();
    if (myTouch.dataAvailable()) {
      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();

      //Back button
      if ((x >= 10) && (x <= 60) && (y >= 10) && (y <= 36)) {
        drawFrame(10, 10, 60, 36);
        currentPage = '0';
        myGLCD.clrScr();
        drawHomeScreen();

      }
    }
  }
}

Hi Koraks,

I used the tft tutorial Here as my template.

Thanks in advance

Steve

Thanks. I had a brief look at it, and while it would be possible to track down the specific bug you're chasing, I would propose a different approach.

The reason why you're running into this problem is that your code is highly integrated and non-hierarchical, which makes it easy to get lost in its structure and difficult to keep track of the mutual dependencies between the parts of code. You might solve the two bugs you ask your question about, but I guarantee you that within half an hour you'll have one or two more similar problems popping up.

I would suggest keeping things more separated, i.e.:

  • distinguish between input and output routines, and try to not combine them in the same functions
  • distinguish between high- and low-level tasks, and try not to do any low-level processing (such as reading out user input directly) in the main loop.
    The combination of both premises above is likely the cause of your problem; for instance, it seems that in the main loop you're calling the setPulsetriggervalue() function which will run regardless if there's user input, and given its initial if-statement the function begins with, it may do manipulation on a variable (xTrigger) which you map to a certain range without actually establishing if you have (at that particular moment in runtime) any meaningful data that you want to map.

The problem with redrawing certain screen elements is also due to the lack of clear structure to the code; in your place, I would work with a finite state machine and clearly define display modes/templates for each particular machine state, and write a routine that updates the display according to the state the machine is currently in. That way you would delegate all this low/medium level processing to a set of core routines that you can troubleshoot separately, and when that's done, you can focus on the higher-level system behavior.

In other words: tracking down your particular bugs would, if it were my project, not have priority, whereas rethinking the architecture of your software would be at the very top of my list.

Thanks Koraks,

I will have a look

Regards

Steve

You're welcome; I hope it helps in any way, even though it's probably not the answer you had hoped for.
For what it's worth, you may look into this: Model–view–controller - Wikipedia
It's an old concept, but it might help you to rethink the basic building blocks of your code.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.