Vítejte na Elektro Bastlírn?
Nuke - Elektro Bastlirna
  Vytvořit účet Hlavní · Fórum · DDump · Profil · Zprávy · Hledat na fóru · Příspěvky na provoz EB

Vlákno na téma KORONAVIRUS - nutná registrace


Nuke - Elektro Bastlirna: Diskuzní fórum

 FAQFAQ   HledatHledat   Uživatelské skupinyUživatelské skupiny   ProfilProfil   Soukromé zprávySoukromé zprávy   PřihlášeníPřihlášení 

Graf ve vizualizačním softwaru Processing (Arduino)

 
Přidat nové téma   Zaslat odpověď       Obsah fóra Diskuzní fórum Elektro Bastlírny -> Miniaturní počítače (Arduino, Raspberry a další)
Zobrazit předchozí téma :: Zobrazit následující téma  
Autor Zpráva
arduino2015



Založen: Jan 30, 2015
Příspěvky: 158

PříspěvekZaslal: ne březen 01 2015, 10:56    Předmět: Graf ve vizualizačním softwaru Processing (Arduino) Citovat

Ahoj, spojením těchto dvou zdrojů (v jednom byly použity zastaralé části kódu, v druhém překlepy) http://ardypro.blogspot.cz/2011/07/processingarduino.html https://boolscott.wordpress.com/2010/02/04/arduino-processing-analogue-bar-graph-2/

Jsem získal tento program pro vykreslování grafu:

kód:
// Feel Free to edit these variables ///////////////////////////
String  xLabel = "Analogue Inputs";
String  yLabel = "Voltage (V)";
String  Heading = "The Graph Sketch";
String  URL = "01/02/2010";
float Vcc = 5.0;    // the measured voltage of your usb
int NumOfVertDivisions=5;      // dark gray
int NumOfVertSubDivisions=10;  // light gray

int NumOfBars=6;    // you can choose the number of bars, but it can cause issues
                    // since you should change what the arduino sends

// if these are changed, background image has problems
// a plain background solves the problem
int ScreenWidth = 600, ScreenHeight=400;
/////////////////////////////////////////////////////////

//  Serial port stuff ///////////////////////
import processing.serial.*;
Serial myPort;
boolean firstContact = false;
int[] serialInArray = new int[6];
int serialCount = 0;
///////////////////////////////////////////////

int LeftMargin=100;
int RightMArgin=80;
int TextGap=50;
int GraphYposition=80;
float BarPercent = 0.4;

int value;

PFont font;
PImage bg;

int temp;
float yRatio = 0.58;
int BarGap, BarWidth, DivisounsWidth;
int[] bars = new int[NumOfBars];

void setup(){

 // bg = loadImage("BG.jpg");

  /// NB SETTINGS ////////////////////////////////////////////////////////
  myPort = new Serial(this, Serial.list()[0], 9600);
  ////////////////////////////////////////////////////////////////////////

  DivisounsWidth = (ScreenWidth-LeftMargin-RightMArgin)/(NumOfBars);
  BarWidth = int(BarPercent*float(DivisounsWidth));
  BarGap = DivisounsWidth - BarWidth;

  size(ScreenWidth,ScreenHeight);
  font = createFont("Arial",12);

  textAlign(CENTER);
  textFont(font);
}

void draw(){

//  background(bg);     // My one used a background image, I've
  background(250);      // commented it out and put a plain colour

  //  Headings();           // Displays bar width, Bar gap or any variable.
  Axis();
  Labels();
  PrintBars();
}

// Send Recieve data //
void serialEvent(Serial myPort) {

  // read a byte from the serial port:
  int inByte = myPort.read();

  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 6 bytes:
    if (serialCount > 5 ) {

for (int x=0;x<6;x++){

  bars[x] = int (yRatio*(ScreenHeight)*(serialInArray[x]/256.0));

}

      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

/////// Display any variables for testing here//////////////
void Headings(){
  fill(0 );
  text("BarWidth",50,TextGap );
  text("BarGap",250,TextGap );
  text("DivisounsWidth",450,TextGap );
  text(BarWidth,100,TextGap );
  text(BarGap,300,TextGap );
  text(DivisounsWidth,520,TextGap );
}

void PrintBars(){

  int c=0;
  for (int i=0;i<NumOfBars;i++){

    fill((0xe4+c),(255-bars[i]+c),(0x1a+c));
    stroke(90);
    rect(i*DivisounsWidth+LeftMargin,   ScreenHeight-GraphYposition,   BarWidth,   -bars[i]);
    fill(0x2e,0x2a,0x2a);
    text(float(bars[i])/(yRatio*(ScreenHeight))*Vcc,   i*DivisounsWidth+LeftMargin+BarWidth/2,   ScreenHeight-bars[i]-5-GraphYposition );
    text("A",   i*DivisounsWidth+LeftMargin+BarWidth/2 -5,   ScreenHeight-GraphYposition+20 );
    text(i,   i*DivisounsWidth+LeftMargin+BarWidth/2 +5,   ScreenHeight-GraphYposition+20 );
  }
}

void Axis(){

  strokeWeight(1);
  stroke(220);
  for(float x=0;x<=NumOfVertSubDivisions;x++){

    int bars=(ScreenHeight-GraphYposition)-int(yRatio*(ScreenHeight)*(x/NumOfVertSubDivisions));
    line(LeftMargin-15,bars,ScreenWidth-RightMArgin-DivisounsWidth+50,bars);
  }
  strokeWeight(1);
  stroke(180);
  for(float x=0;x<=NumOfVertDivisions;x++){

    int bars=(ScreenHeight-GraphYposition)-int(yRatio*(ScreenHeight)*(x/NumOfVertDivisions));
    line(LeftMargin-15,bars,ScreenWidth-RightMArgin-DivisounsWidth+50,bars);
  }
  strokeWeight(2);
  stroke(90);
  line(LeftMargin-15, ScreenHeight-GraphYposition+2, ScreenWidth-RightMArgin-DivisounsWidth+50, ScreenHeight-GraphYposition+2);
  line(LeftMargin-15,ScreenHeight-GraphYposition+2,LeftMargin-15,GraphYposition);
  strokeWeight(1);
}

void Labels(){
  textFont(font,18);
  fill(50);
  rotate(radians(-90));
  text(yLabel,-ScreenHeight/2,LeftMargin-45);
  textFont(font,16);
  for(float x=0;x<=NumOfVertDivisions;x++){

    int bars=(ScreenHeight-GraphYposition)-int(yRatio*(ScreenHeight)*(x/NumOfVertDivisions));
    text(round(x),-bars,LeftMargin-20);
  }

  textFont(font,18);
  rotate(radians(90));
  text(xLabel,LeftMargin+(ScreenWidth-LeftMargin-RightMArgin-50)/2,ScreenHeight-GraphYposition+40);
  textFont(font,24);
  fill(50);
  text(Heading,LeftMargin+(ScreenWidth-LeftMargin-RightMArgin-50)/2,70);
  textFont(font);

  fill(150);
  text(URL,ScreenWidth-RightMArgin-40,ScreenHeight-15);
  textFont(font);

}


Pro úplnost, uvádím i protikus pro Arduino:

kód:
//Sending 8 bit reading (256) so analogue
//reading can be sent in 1 serial.write() instead

int Analogue0 = 0;    // first analog sensor
int Analogue1 = 0;   // second analog sensor
int Analogue2 = 0;    // digital sensor
int Analogue3 = 0;   // second analog sensor
int Analogue4 = 0;   // second analog sensor
int Analogue5 = 0;   // second analog sensor
int inByte = 0; // incoming serial byte        // incoming serial serial.write() instead

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);

  establishContact();  // send a serial.write() instead to establish contact until Processing responds
}

void loop()
{
  // if we get a valid serial.write() instead, read analog ins:
  if (Serial.available() > 0) {
     // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    Analogue0 = analogRead(0)/4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    Analogue1 = analogRead(1)/4;
    // read  switch, multiply by 155 and add 100
    // so that you're sending 100 or 255:
    delay(10);
    Analogue2 = analogRead(2)/4;
    delay(10);
    Analogue3 = analogRead(3)/4;
    delay(10);
    Analogue4 = analogRead(4)/4;
    delay(10);
    Analogue5 = analogRead(5)/4;
    delay(10);

    // send sensor values:  0451 je to dost pošahaný, takže bacha
 Serial.write(Analogue0 );
 Serial.write(Analogue1 );
 Serial.write(Analogue2 );
 Serial.write(Analogue3 );
 Serial.write(Analogue4 );
 Serial.write(Analogue5 );
  }
}

void establishContact() {
 while (Serial.available() <= 0) {
 delay(300);
  }
}


Graf se ovšem nemění... četl jsem kód, a podle mě to funguje tak, že nejdříve po sériové lince putuje testovací signál, znak "A", a pak by měly putovat hodnoty z analogového senzoru. Ovšem, když použiji serial monitor v Arduino IDE, vidím jen samá áčka, donekonečna.

HW problémem není, zkoušel jsem použít jednoduší kombinaci programů pro A a P, a fungovalo to bez problémů.

Jako testovací zapojením mám otočný potenciometr v režimu odporového děliče (pravá noha 5V, prostřední A0 a levá na GND). Používám NANO.

Nějaké nápady?
Návrat nahoru
Zobrazit informace o autorovi Odeslat soukromou zprávu
Jurik-EB



Založen: Oct 02, 2009
Příspěvky: 206
Bydliště: Rožnov pod Rad.

PříspěvekZaslal: so červenec 25 2015, 20:26    Předmět: Citovat

Serial.write uprav takto a budeš to mít monitoru Arduina

Serial.write(Analogue0 ); Serial.print("An0 = ");Serial.println(Analogue0);
Serial.write(Analogue1 ); Serial.print("An1 = ");Serial.println(Analogue1);
Serial.write(Analogue2 ); Serial.print("An2 = ");Serial.println(Analogue2);
Serial.write(Analogue3 ); Serial.print("An3 = ");Serial.println(Analogue3);
Serial.write(Analogue4 ); Serial.print("An4 = ");Serial.println(Analogue4);
Serial.write(Analogue5 ); Serial.print("An5 = ");Serial.println(Analogue5);

Serial.println();Serial.println();
Návrat nahoru
Zobrazit informace o autorovi Odeslat soukromou zprávu Odeslat e-mail
Zobrazit příspěvky z předchozích:   
Přidat nové téma   Zaslat odpověď       Obsah fóra Diskuzní fórum Elektro Bastlírny -> Miniaturní počítače (Arduino, Raspberry a další) Časy uváděny v GMT + 1 hodina
Strana 1 z 1

 
Přejdi na:  
Nemůžete odesílat nové téma do tohoto fóra.
Nemůžete odpovídat na témata v tomto fóru.
Nemůžete upravovat své příspěvky v tomto fóru.
Nemůžete mazat své příspěvky v tomto fóru.
Nemůžete hlasovat v tomto fóru.
Nemůžete připojovat soubory k příspěvkům
Můžete stahovat a prohlížet přiložené soubory

Powered by phpBB © 2001, 2005 phpBB Group
Forums ©
Nuke - Elektro Bastlirna

Informace na portálu Elektro bastlírny jsou prezentovány za účelem vzdělání čtenářů a rozšíření zájmu o elektroniku. Autoři článků na serveru neberou žádnou zodpovědnost za škody vzniklé těmito zapojeními. Rovněž neberou žádnou odpovědnost za případnou újmu na zdraví vzniklou úrazem elektrickým proudem. Autoři a správci těchto stránek nepřejímají záruku za správnost zveřejněných materiálů. Předkládané informace a zapojení jsou zveřejněny bez ohledu na případné patenty třetích osob. Nároky na odškodnění na základě změn, chyb nebo vynechání jsou zásadně vyloučeny. Všechny registrované nebo jiné obchodní známky zde použité jsou majetkem jejich vlastníků. Uvedením nejsou zpochybněna z toho vyplývající vlastnická práva. Použití konstrukcí v rozporu se zákonem je přísně zakázáno. Vzhledem k tomu, že původ předkládaných materiálů nelze žádným způsobem dohledat, nelze je použít pro komerční účely! Tento nekomerční server nemá z uvedených zapojení či konstrukcí žádný zisk. Nezodpovídáme za pravost předkládaných materiálů třetími osobami a jejich původ. V případě, že zjistíte porušení autorského práva či jiné nesrovnalosti, kontaktujte administrátory na diskuzním fóru EB.


PHP-Nuke Copyright © 2005 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Čas potřebný ke zpracování stránky 0.12 sekund