100 question on Simple oscilloscope with Arduino code

Members

//https://www.instructables.com/id/How-to-Create-a-Simple-Oscilloscope-With-Arduino-a/
/*The screens got a yellow bar graph that moves with the value
   the value is printed before the :ADC

*/
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
MCUFRIEND_kbv tft;

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
//Touchscreen Y+ X- Y- X+ pins
#define YP A5
#define XM A4
#define YM 10
#define XP 11
//tested as correct for new ADAFRUIT screen
#define TS_MINX 150
#define TS_MINY 90
#define TS_MAXX 900
#define TS_MAXY 800
/* **** TFT colors *************** */
#define BLACK       0x0000
#define BLUE        0x001F
#define CYAN        0x07FF
#define DARKCYAN    0x03EF
#define GREY1       0x8410
#define GREY2       0x4208
#define LIGHTGREY   0xC618
#define DARKGREY    0x7BEF
#define GREEN       0x07E0
#define DARKGREEN   0x03E0
#define GREENYELLOW 0xB7E0
#define MAGENTA     0xF81F
#define MAROON      0x7800
#define NAVY        0x000F
#define OLIVE       0x7BE0
#define ORANGE      0xFDA0
#define PINK        0xFC9F
#define PURPLE      0x780F
#define RED         0xF800
#define BRIGHT_RED  0xf810
#define WHITE       0xFFFF
#define YELLOW      0xFFE0

/* ******************************** */

void setup(void) {
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  //tft.fillRect(0, 0,320 , 240, BLACK);//fill screen black
  //tft.drawRect(5, 140, 165, 100, WHITE);//outer frame on graph
  //tft.drawRect(9, 9, 202, 40, GREEN);//frame of the bar graph
  //tft.drawLine(10,90, 10, 240, BLUE);//vertical line on the left
  //tft.drawLine(5, 230, 320, 230, RED);//line at bottom of screen
  tft.setTextColor(GREEN);
  tft.setTextSize(2);
  //tft.setCursor(215, 15);tft.println("%100");
  tft.setCursor(100, 55);
  tft.println(" :ADC");
}

void loop() {
  int j, i;
  j = analogRead(A6); //fetch reading
  //i = map(j, 0, 1023, 1, 200);//something to do with the moving graph bar
  //by changing these values the size is changed
  //tft.fillRect(10, 10, i, 10, WHITE); //draw the moving bar graph rect
  //tft.fillRect(i + 9, 10, (200 - i + 1), 10, BLACK); //erase the prev moving bar graph data
  tft.fillRect(10, 50, 100, 40, BLACK);//erase prev numeric value before :ADC
  tft.setCursor(10, 55);
  tft.println(j);//prints the numeric value before :ADC
  print_x(map(j, 0, 1023, 227, 90));
  delay(400);//sample write delay. change this later and use millis
}// end loop()

int x = 12;//starting point on the X axis
void print_x(int h) {
  //change values to make dot bigger/smaller
  tft.drawLine(x , h , x , h + 2, CYAN); //draws the actual dot of the graph
  if (x < 160)x++;//change the length of the X axis value of the graph
  else {
    x = 12;//starting point on the X axis
    //Left,Top,Length, height
    tft.fillRect(6, 142, 155, 95, BLACK);//erase the prev graph if (x > 161)
  }
}//end void print_x

I am not sure where/how to adjust the y-axis height.

I have heaps of questions on this code but I’ll go through it slowly so I can understand it better.

Thanks

Here?print_x(map(j, 0, 1023, 227, 90));

TheMemberFormerlyKnownAsAWOL:
Here?print_x(map(j, 0, 1023, 227, 90));

j= the pin reading
0= 0 Volt
1023= 5 Volt
227=?
90=?

Thanks

fnb111:
j= the pin reading
0= 0 Volt
1023= 5 Volt
227=?
90=?

Thanks

227 and 90 are the maximum and minimum values for 'y' on your display. The map instruction is used to invert the scale the analog input; a reading of 0V -> y=227 and a reading of 5V -> y=90.

If the upper-left corner of your LCD is (0,0) then y=227 is "lower" on the display than y=90 (so 0V dots are further down than 5V dots...)

If you want to "zoom" in you'll probably want to map the viewing height (90 to 227) to a smaller range of the DC input. For example, 2x zoom might be:

print_x( map( j, 256, 768, 227, 90) );

You could add an offset potentiometer to make the zoomed window "sliding" by using it to adjust the limits 256 and 768 in the map() call.

Thanks a lot for that Blackfin. Well explained. I use a 240 height x 320 long tft. If I move the 227 upwards,say 180, I would move the 0 Volt point higher up on my screen.

How do I change the code to read a +5 Volt and -5 Volt?

fnb111:
How do I change the code to read a +5 Volt and -5 Volt?

-5 V with respect to what?

5V ..
.
'
'
' .
0V -------------------------------------------------------------------------
'.
. .
. .
-5V ...

something like this?

Does your ADC read negative voltages?

TheMemberFormerlyKnownAsAWOL:
Does your ADC read negative voltages?

I use a DUE and no. I dont think it can read a negative input on the pin. The input I am going to use in the future would be from a nRF witch does have a negative reading.