Operácie

MEMS cvičenie 4

Z SensorWiki

Verzia z 21:57, 11. marec 2020, ktorú vytvoril Balogh (diskusia | príspevky)
OpenSegmentDisplay.jpg

Cieľom tohoto cvičenia je zobraziť meranú veličinu na grafickom displeji v počítači.

Úlohy boli 10.3.2020 upravené na možnosť pracovať aj doma.

Meranú veličinu odmeriame procesorom, po prevode A/D prevodníkom a úprave linearizáciou z predošlých cvičení budeme hodnotu posielať po sériovej linke s parametrami 9600-N-8-1 do PC. Ak máte doma Arduino, môžete použiť program na vysielanie hodnoty z potenciometra pripojeného na niektorý analógový vstup, alebo len posielať náhodne vygenerované hodnoty. Program môže vyzerať napríklad takto:

int adcValue = 0;

void setup() {

      Serial.begin(9600);
}


void loop() 
{

 /* This part of code is for real measurement from Channel 0 */

  adcValue = 0;                           // Filter - average from 16 values 
  for (int i = 1; i<=16; i++)
    adcValue = adcValue + analogRead(0);
  adcValue = adcValue/16;
 
/* Uncomment this part of code, if you want just simulated values */
/*
     if (adcValue < 1023)
          adcValue = adcValue + random(-1,2);
      else
          adcValue = 0;
*/
 
  char tempString[10];                     // Used for sprintf
  sprintf(tempString, "%4d",adcValue);     // Convert into a string that is right adjusted
  //sprintf(tempString, "%d", adcValue);   // Convert into a string that is left adjusted (requires digit 1 command)
  //sprintf(tempString, "%04d", adcValue); // Convert into a string with leading zeros
  //sprintf(tempString, "%4X", adcValue);  // Convert int  HEX, right adjusted
  
 
  Serial.println(tempString);              // Send the value using serial to PC

  delay(100);
}


Hodnoty, ktoré PC prijíma si môžete pozrieť v terminálovom okne priamo v prostredí Arduino, alebo použite radšej profesionálny sériový terminál (napr.

Ak hodnoty chodia tak ako potrebujeme (skontrolujte nastavenie parametrov rýchlosť-parita-počet bytov-počet stopbitov) navrhneme si krajší zobrazovací program (appku, ak chcete).

Na to využijeme prostredie Processing, ktoré vyzerá veľmi podobne ako to Arduinovské, ale generuje program pre Windows (alebo aj pre linux, či iOS). Prostredie Processing je na počítačoch v laboratóriu už nainštalované, doma si môžete nainštalovať odtiaľto: https://processing.org/

Okrem toho si stiahnite a nainštalujte do svojho OS tento pekný font https://fontlibrary.org/en/font/segment7 ktorým budeme veličinu vypisovať.

Arduino code for the simple potentiometer measurement with the local displaying of the value on the OpenSegment display.

Corresponding Processing code for receiving the value from serial port and displaying it in the window same way as the OpenSegment did.

import processing.serial.*;

 int adcValue = 0;        // value received from Serial 
 // String Unit="mA";
 // String Unit="kΩ";
 // String Unit="°C";     // We can use Unicode chars directly
 String Unit="\u00B0C";   // Unicode codes may be entered as well
 
 Serial myPort;
 PFont Segment, Units; 

 void setup() {
                          // Setup the display window 
  size(480, 180);         // Size of the window
  Segment = createFont("Segment7", 150);  // Assign fonts and size 
  Units = createFont("Arial", 40);
  textFont(Segment);
  textAlign(RIGHT);        // Text align 
  fill(250,250,0);         // Font color is yellow = red + green

  println(Serial.list());    // List all the available serial ports
  // Then open the port  you're using, my is the first, i.e. '0'
   myPort = new Serial(this, Serial.list()[0], 9600);
 // don't generate a serialEvent() unless you get a newline character:
   myPort.bufferUntil('\n');
 }

void draw()
{                      // Let's start to display
  background(0,0,0);   // set the background color black

  textFont(Segment);
  text(adcValue, 400, 150);
  
  textFont(Units);
  text(Unit,465,65);
}


void serialEvent(Serial myPort) 
{
 String inString = myPort.readStringUntil('\n');    // get the ASCII string:
 if (inString != null) 
    {
     inString = trim(inString);     // trim off any whitespace
     adcValue = int(inString);      // convert into an integer
     adcValue =int(map(adcValue, 0, 1023, 0, 1023));  // possible range adjusting
    }
 }


OpenSegmentScreenshot.png


Ak nemáte Arduino, môžete si program v Processingu vyskúšať aj tak. Nainštalujete si dvojicu virtuálnych sériových portov napr. https://www.eltima.com/products/vspdxp/ - stačí základnú verziu Standard, ktorá je bezplatne k dispozícii 14 dní. V systéme vám vzniknú dva nové sériové porty. Jeden otvoríte v termináli, cez neho budete posielať čísla, ktoré chcete zobraziť. Druhý port použijete ako prijímací v Processingu.