Pages: [1]   Go Down
Author Topic: Cant seem to map  (Read 390 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I have just started using processing to display some data from a temp/ rh sensor, id much prefer to use c++ as thats my native language but i like processings quickness, I send the data from the arduino as an Int, it gets displayed on the screen but if the temp rises 10C the graph rises too but only by a little bit, I have 150 pixels to work with so i thought mapping the temp from a range of 0 - 80 for temp and the RH from 0 - 100, when I use the map function it displays a cant use map on float type, its all int's im working with so I dont get it?

here is my code, it has unused vars all over the place as it was just a quick sketch, id like to map the incoming data during the process serial function and just add the mapped data to the data array as the raw data is going to a file.

Code:

import processing.serial.*;
Serial myport;
PrintWriter ExcelGraphData;

PFont g_font;

int Temp_C, Temp_F, RH, light_level, date_day, date_month, date_year, time_hours, time_minutes;

// Max/Min temp vars
int Max_Temp, Min_Temp, Max_RH, Min_RH;
String All_Time = "";
String All_Date = "";
String Max_temp_time = "";
String Min_temp_time = "";
String Max_RH_time = "";
String Min_RH_time = "";

GraphData Array_Data = new GraphData(620);
Draw_data Drawgraph = new Draw_data(0);




void setup(){
  // Screen Layout, draw once.
 
  size(800,500);
  g_font = loadFont("JasmineUPCBold-60.vlw");
  background(0,0,0);
  ExcelGraphData = createWriter("ExcelGraphData.csv");
  delay(1000);
  myport = new Serial(this, "COM3", 9600);
  myport.bufferUntil('.');
 
}

void draw(){
 
  delay(1000);
  background(0,0,0);
  strokeWeight(3.5);
  stroke(255,255,204);  line(0, 80, 800, 80);
  strokeWeight(1.5);
  stroke(255,255,204);  line(180, 80, 180, 380);
  stroke(255,255,204);  line(0, 380, 800, 380);
  strokeWeight(0.2);
  stroke(255,255,204);  line(180, 230, 800, 230);
  textFont(g_font);

  fill(255,0,0);
  text(All_Time, 520, 40);
 
 
  text(All_Date, 620, 40);
 
  text("Version 0.1", 40 , 45);
  textFont(g_font, 60);
  fill(153,255,255);
  text("TempC", 25, 170);
  textFont(g_font, 60);
  text("RH%", 45, 320);
  textFont(g_font, 20);
  text("Current Temperature / RH%", 200, 400);
  text("Max Temp", 420, 400);
  text("Min Temp", 510, 400);
  text("Max RH%", 600, 400);
  text("Min RH%", 690, 400);
  textFont(g_font, 20);
  text(Temp_C, 200, 420);text("C", 220, 420);
  text(Temp_F, 200, 435);text("F", 220, 435);
  text(RH, 200, 450); text("%", 220, 450);

  Drawgraph.drawline(Array_Data);
}

void serialEvent(Serial myPort){
  int mapped_tempc = 0;
  int mapped_rh = 0;
  String myString = myport.readStringUntil(46);
  myString = myString.substring(0, myString.length() - 1);
  if(myString != null ){
    myString = trim(myString);
    int inputs[] = int(split(myString, ','));
    //assign values for processing
      Temp_C = inputs[0];
      Temp_F = inputs[1];
      RH = inputs[2];
      date_day = inputs[3];
      date_month = inputs[4];
      date_year = inputs[5];
      time_hours = inputs[6];
      time_minutes = inputs[7];
      //mapped_tempc = map(Temp_C, 0, 70, 0, 150);
      //mapped_rh = map(RH, 0, 100, 0, 150);
      Array_Data.addval(Temp_C, RH);
     
     
     
           
  }
  if(time_minutes < 10)
  {
    All_Time = time_hours + ":" + "0" + time_minutes;
  }
  else
  {
    All_Time = time_hours + ":" + time_minutes;
  }
  All_Date = date_day + "/" + date_month + "/" + date_year;
  ExcelGraphData.print(Temp_C + " ," + RH + "\n");
  ExcelGraphData.flush();
}

class GraphData{
  int Max_Size = 620;
  int[] temp;
  int[] rh;
  int curspos;
  GraphData(int m_max_size){
    Max_Size = m_max_size;
    temp = new int [Max_Size];
    rh = new int [Max_Size];
    curspos= 0;
  }

  int get_curspos(){return curspos;}
  int set_curspos(int i){curspos = i; return 0;}
  void addval(int t, int r)
  {
    if(curspos < 620){
    temp[curspos] = t;
    rh[curspos] = r;
    curspos++;
    }
    if (curspos == Max_Size)
    {
      curspos = 0;
    }
  }
  int getval(int type, int v)
  {
    if(type == 0)
    {
      return temp[v];
    }
    if(type == 1)
    {
      return rh[v];
    }
    if (curspos == 620)
    {
      curspos = 0;
      background(0);
    }
    return 0;
  }
};
 
class Draw_data
{
  int startpos_x;
  int v;
  Draw_data(int cursorpos){
  startpos_x = 230;
  v = 0;
  }
    void drawline(GraphData Array_Data)
    {
      int x,y,w,z;
      int startposx = 180;
     
      int startposy= 230;
      int startposy1 = 380;
      x=0;
     
      for(int i = 0; i < Array_Data.get_curspos() + 1; i++){
        /*int valtemp = Array_Data.getval(0,x);
        y = map(valtemp, 0, 70, 0, 150);
      w = map(Array_Data.getval(1,x), 0, 100, 0, 150);*/

       fill(150,150,255);
      strokeWeight(1.0);
      line(startposx, startposy, startposx, startposy - Array_Data.getval(0,x));
      line(startposx, startposy1, startposx, startposy1 - Array_Data.getval(1,x));
      if(startposx == 620 && x == 620)
      {
        startposx = 180;
        x = 0;
        background(0);
      }
      else
      {
        startposx++;
        x++;
      }
      }
    }
};



regards
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50505
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  String myString = myport.readStringUntil(46);
I think that
Code:
  String myString = myport.readStringUntil('.');
is much easier to understand/modify.

Quote
i thought mapping the temp from a range of 0 - 80 for temp and the RH from 0 - 100, when I use the map function it displays a cant use map on float type, its all int's im working with so I dont get it?
That's not what it says. It says it can not convert a float to an int. The map function in Processing returns a float, which you are trying to store in an int. You must tell Processing that doing so is OK:
Code:
      mapped_tempc = (int)map(Temp_C, 0, 70, 0, 150);
      mapped_rh = (int)map(RH, 0, 100, 0, 150);
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That worked, thanks.  I thought map was all int maths.
Logged

Pages: [1]   Go Up
Jump to: