Sunday, November 10, 2019

faster CSV grapher for nRF24L01

//import io.inventit.processing.android.serial.*;

import processing.serial.*;
import static javax.swing.JOptionPane.*;
//import processing.net.*;
//Client ennodaClient;
Serial envazhi;
int nikazhmadhippu1, nikazhmadhippu2, nikazhmadhippu3;
int z=10;

int[] akalavu;
int[] meiyalavu;
int[] uyiralavu;
int ullpiri;
int i,d,p;
//int value=20;

void setup()
{
 
 
 
 //  fill(180, 0, 0);
 // for (d=0; d<1080; d++){
 // text ("rajaguru", 600, d); }
 
  //text ("rajaguru", 600, 800);
 
 
  fullScreen();
  //size(1000, 550);
  frameRate(60);
   
  strokeWeight(1);
  smooth(); // or noSmooth();
  akalavu = new int[200];
  meiyalavu = new int[200];
  uyiralavu = new int[200];
  String portName = Serial.list()[0];
  envazhi = new Serial(this, portName, 230400);

 // text("STOP",1855,30);
 
 
 
}

void draw()
{
 
  fill(0, 0, 180);
  if (d<300)
   {
    background(255);
    //textSize(5+d);
    textSize(20);
    text ("rajaguru", 1920-d, 600);
    d+=5;
   }
  fill(255,255, 255);
 
 
  //text("STOP",1855,20);
  //fill(20);
  line(1850,10, 1900, 50);
  line(1900,10, 1850, 50);
  line(1900,10, 1900, 50);
  line(1900,50, 1850, 50);
  line(1850,10, 1850, 50);
  line(1850,10, 1900, 10);
 
  textSize(12);
 
 
 
  String ull = envazhi.readStringUntil('\n');

  if(ull != null)
  {
     
   int[] ullpiri = int(split(ull, ","));
 
   nikazhmadhippu1 = abs(int(ullpiri[0]));
   nikazhmadhippu2 = abs(int(ullpiri[1]));
   nikazhmadhippu3 = abs(int(ullpiri[2]));
   background(255);
   int lowest = min(ullpiri);
   int highest = max(ullpiri);
   p++;
   println( p);
 
   
   {
 
     for(int i = 1; i < 200; i++)
        {
       
          akalavu[i-1] = akalavu[i]; //shifting every value to previous array-element ANDDDDDDDDD storing 200 subsequent values in  200 array-elements
          meiyalavu[i-1] = meiyalavu[i];
          uyiralavu[i-1] = uyiralavu[i];
        }
       
             
       // print(lowest);
       // print(",");
       // println(highest);
      //  println();
       
   akalavu[200-1] = int (map(nikazhmadhippu1, lowest, highest, 20, 500)) ;
   meiyalavu[200-1] = int( map(nikazhmadhippu2, lowest, highest, 400, 1000));
   uyiralavu[200-1] = int( map(nikazhmadhippu3, lowest, highest, 20, 600));

  }
 
    {     
     beginShape();
      strokeWeight(2);
      stroke(0, 0, 250); //(rgb) colored lines, blue for z-axis
      {
       int j=1;
       int i=1;
      // int d=0;
      //plotting allllllllllllllllll 100 array-elements in x-y graph here.       
      // variable-i gives 100 numbers of array-elements which are plotted on the y-axis; variable-j gives the x-axis position.
      // variable z helps to increase or decrease the x-axis-gap between 2 successive inputs.
       for (i=1, j=1; i<199 && j <(z*200); i++, j= j+z)
        {   
         
  //if (d<10800){
 // for (d=0; d<10800; d+=10){
 // text ("rajaguru", 600, d/10); }}
         
           if (akalavu[i-1]>0){
         curveVertex(j, akalavu[i-1]); //as the loop runs, on every loop a single vertex-point is fixed in the display-window-matrix and....on next loop another vertex-point is fixed. and those 2 points get linked by a curve.
                 
           }
         
        }                             //this logic of curve-fitting between 2 successive points continues till the end of THIS "for" loop, resulting in the curve-plot.
     
      }
     endShape();   
    }

    //x-axis
   
      {     
      beginShape();
      strokeWeight(2);
      stroke(0, 250, 0); //green for x-axis
       {
       int j=1;
       int i=1;
     
       for (i=1, j=1; i<199 && j <(z*200); i++, j= j+z)
        {           
          if (uyiralavu[i-1]>0){
          curveVertex(j, uyiralavu[i-1]); //as the loop runs, on every loop a single vertex-point is fixed in the display-window-matrix and....on next loop another vertex-point is fixed. and those 2 points get linked by a curve.
         
         
         }
        }                           
   
       }
      endShape();   
      }
   
    //y-axis 
   
      {     
      beginShape();
      strokeWeight(2);
      stroke(250, 0, 0); //red for y-axis
       {
       int j=1;
       int i=1;
   
       for (i=1, j=1; i<199 && j <(z*200); i++, j= j+z)
        {                                             
         if (meiyalavu[i-1]>0){
         curveVertex(j, meiyalavu[i-1]);
         }
        }                           
     
       }
      endShape();   
      }
 
  }
 
 nEram();
  framerate();
 //name();

}

//void name() {
 // PImage padam1, padam2, padam3, padam4, padam5, padam6, padam7;
  //fill(80, 80, 200);
 // text ("rajaguru", 1550, 100);
  //fill(255, 255, 255);
  //padam2 = loadImage("enperu.png");
  //image(padam2, 1000, 0, 129, 29);
 // padam1 = loadImage("tamilsource.png");
 // image(padam1, 10, 5, 50, 50);
  //padam3 = loadImage ("rate.png");
 // image(padam3, 215, 15, 325, 28);
  //padam4 = loadImage ("count.png");
  //image(padam4, 248, 43, 292, 25);
  //padam5 = loadImage ("duration.png");
  //image(padam5, 200, 68, 340, 28);
  //padam6 = loadImage ("time.png");
  //image(padam6, 333, 93, 207, 26);
  //padam7 = loadImage ("axes.png");
  //image(padam7, 70, 5, 108, 115);
 
  //image(padam2, 0, 230, 100, 20);
//}

void nEram(){
  textSize(15);
  fill(180, 0, 0);
 // int maNi = hour();
 // int nimidam = minute();
 // int nodi = second();

  //text (maNi, 550,110);
  //text (": "+ nimidam, 570, 110);
  //text ( " : " + nodi, 590, 110);
  //text ("hrs", 620,110);
  //text (System.nanoTime(), 620,110);
  //text (System.currentTimeMillis(), 620,110);
  fill(0, 0, 180);
  text (nikazhmadhippu1, 80, 33,10);
  //text ("rajaguru", 980,20);
  fill(180, 0, 0);
  text (nikazhmadhippu2, 120, 33, 10);
  fill(0, 180, 0); //green
  text (nikazhmadhippu3, 160, 33,10);
 
  fill(0, 0, 255);

}

void framerate(){
  text (frameRate, 545, 33);
 
  //text ("frame-eNNikkai / count:", 500, 55);
  //text ("Odum-nEram, nodiyil //running-duration, sec: ", 380, 40);
  text (frameCount, 630, 33);
  text (millis()/1000, 500, 33);
  fill(255, 255, 255);
}
void mouseClicked(){
  if ((mouseX>1850&&mouseX<1900)&&(mouseY>10&&mouseY<50))
     {
       //showMessageDialog(null, "do you want to close?!!!",
   // "Alert", ERROR_MESSAGE);
   
    int reply = showConfirmDialog(null, "want to close?!", "Usaaru / Alert",YES_NO_OPTION);
        if (reply == YES_OPTION)
        { exit();
          showMessageDialog(null, "bye!", "Seidhi / Message", ERROR_MESSAGE);
        }
       // else {
           //showMessageDialog(null, "do u want to close?!");
           //System.exit(0);
       // }
   
  //  if (keyPressed) {
   // if (key == 'y' || key == 'Y') {exit();}}
  }
}


  

nRF24L01_Rx

#include <SPI.h>

#include <RF24_config.h>
#include <RF24.h>
#include <printf.h>
#include <nRF24L01.h>

RF24 radio(7,8);
byte addresses[][6] = {"12"};

typedef struct
{
  int x, y, z;
}dataStruct;

 dataStruct data;

 void setup()
 {
   Serial.begin(230400);
   radio.begin();
   radio.openReadingPipe(0, addresses[0]);
   radio.startListening();
 }

 void loop()
 {
   if(radio.available())
   {
     radio.read(&data,sizeof(data));
   
     Serial.print(data.x);
     Serial.print(",");
     Serial.print(data.y);
     Serial.print(",");
     Serial.print(data.z);
     Serial.print(",");
     Serial.println();
   }
 }

adxl345_and_335_combined_tx_230400

#include <SPI.h>

#include <RF24_config.h>
#include <RF24.h>
#include <printf.h>
#include <nRF24L01.h>
#include <Wire.h>

RF24 radio(7,8);
byte addresses[][6] = {"12"};

#define DEVICE (0x53) // Device address as specified in data sheet

byte _buff[6];

char POWER_CTL = 0x2D;  //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
int x,y,z;
const int buttonPin = 2;
int buttonState = 0; 

typedef struct
{
  int x, y, z;
}dataStruct;

 dataStruct data;

 void setup()
 {
   pinMode(buttonPin, INPUT);
   radio.begin();
   radio.openWritingPipe(addresses[0]);

    Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(230400);  // start serial for output. Ensure Serial Monitor at same rate
 // Serial.print("init");
 
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeTo(DATA_FORMAT, 00001000);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeTo(POWER_CTL, 0x08);
 
 }
 void loop()
 {

   buttonState = digitalRead(buttonPin);
   //data.x = x;
   //data.y = y;
   //data.z = z;
   if (buttonState == HIGH) {
   readAccel(); // read the x/y/z tilt
 
 
   radio.write(&data,sizeof(data));
 
   delay(50);}
 
   else
   {
    data.x = analogRead(A0);
    data.y = analogRead(A1);
    data.z = analogRead(A2);
 
   radio.write(&data,sizeof(data));
 
   delay(50);}
 
 
 }
 void readAccel() {
  uint8_t howManyBytesToRead = 6;
  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345

  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  // thus we are converting both bytes in to one int
  int x = (((int)_buff[1]) << 8) | _buff[0]; 
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];


  data.x = x;
   data.y = y;
   data.z = z;

 
 
 
   radio.write(&data,sizeof(data));
 
 
 // Serial.print("x: ");
  Serial.print( x );
  Serial.print(",");
  Serial.print( y );
  Serial.print(",");
  Serial.println( z );
}

void writeTo(byte address, byte val) {
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // send register address
  Wire.write(val);                 // send value to write
  Wire.endTransmission();         // end transmission
}

// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[]) {
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // sends address to read from
  Wire.endTransmission();         // end transmission

  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device

  int i = 0;
  while(Wire.available())         // device may send less than requested (abnormal)
  {
    _buff[i] = Wire.read();    // receive a byte
    i++;
  }
  Wire.endTransmission();         // end transmission
}