Saturday, May 16, 2015

MuBox Code and Such

The above two scripts link together two of the boards, having a repeater at each fourth pin on the board so that the data is properly spaced. The main computer side has 3 simple python programs, which are really just reiterations of eachother for each serial port. It could all be integrated but, why, really?

import serial
ser=serial.Serial('/dev/ttyAMC0',9600)

while 1:
 ser.readline();
text_file = open("Output.txt", "w")
text_file.write(ser.readline())
text_file.close()
 
The line defining ser is the part that is modfied, along wiht the output *.txt file. In order to find the Serial ports on linux, just pull each arduino out individually, or plug them individually and run the following code at each iteration:

ls /dev/tty*

this will list a lookup of relevant connections:

The missing or newly added board is the Target. Ussually it will be ACM0, ACM1, USB0, or something very similar.

--------------------------------------------------------------------------------------------------------------------------------------

The Boogie Boards

The boards, I used three, two of the Uno(6 Analog pins each) and one Mega(16 Analog pins). These boards can be purchased from a spinoff company in china which prints boards that rely on the Surface Mount Chips of the ATMEGA IC's.

Anyway, each board needs its own code, for now we can just examine the Uno's as the MEGA is simply a more magnificient Uno.:



CODE:


int sensorPin = A0;        
float sensorValue = 0;
int sensorPinB = A1;        
float sensorValueB = 0;
int sensorPinC=A4;
float sensorValueC=0;
int sensorPinD=A5;
float sensorValueD=0;
float dA=0;
void setup() {
 Serial.begin(9600); 
// Serial.println();
}
void loop() {
  sensorValue = analogRead(sensorPin);
  sensorValue=(sensorValue+analogRead(sensorPin));
   sensorValue=(sensorValue+analogRead(sensorPin));
  sensorValue=(sensorValue+analogRead(sensorPin));
    sensorValue=(sensorValue+analogRead(sensorPin));
   sensorValue=(sensorValue+analogRead(sensorPin));
  sensorValue=(sensorValue+analogRead(sensorPin));
  
if(sensorValue!=0.00){
  
  

  if(1==1){
    //Serial.println("A");
    Serial.println((sensorValue/22)-dA);
    // derivatives: float dA=sensorValue/22;
  }
sensorValue=0;
}
//Lets get one going
  sensorValueB = analogRead(sensorPinB);
  sensorValueB=(sensorValueB+analogRead(sensorPinB));
   sensorValueB=(sensorValueB+analogRead(sensorPinB));
  sensorValueB=(sensorValueB+analogRead(sensorPinB));
    sensorValueB=(sensorValueB+analogRead(sensorPinB));
   sensorValueB=(sensorValueB+analogRead(sensorPinB));
  sensorValueB=(sensorValueB+analogRead(sensorPinB));
if(sensorValueB!=0.00){
  
  

  if(1==1){
   // Serial.println("");
    Serial.println((sensorValueB/22));
  }
sensorValueB=0;
}



//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC



  sensorValueC = analogRead(sensorPinC);
  sensorValueC=(sensorValueC+analogRead(sensorPinC));
   sensorValueC=(sensorValueC+analogRead(sensorPinC));
  sensorValueC=(sensorValueC+analogRead(sensorPinC));
    sensorValueC=(sensorValueC+analogRead(sensorPinC));
   sensorValueC=(sensorValueC+analogRead(sensorPinC));
  sensorValueC=(sensorValueC+analogRead(sensorPinC));
if(sensorValueC!=0.00){
  
  


    //Serial.println("C");
    Serial.println((sensorValueC/22));
  
sensorValueC=0;
}
//Port 4(D)
//Lets get one going
  sensorValueD = analogRead(sensorPinD);
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
   sensorValueD=(sensorValueD+analogRead(sensorPinD));
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
    sensorValueD=(sensorValueD+analogRead(sensorPinD));
   sensorValueD=(sensorValueD+analogRead(sensorPinD));
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
if(sensorValueD!=0.00){
  
  

  if(1==1){
  //  Serial.println("D");
    Serial.println((sensorValueD/22));
  }
sensorValueD=0;
}


}

So basically, we reduce the data count and the variance we get as shown in the histogram at top by sampling multiple data points, or the:
  sensorValueD = analogRead(sensorPinD);
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
   sensorValueD=(sensorValueD+analogRead(sensorPinD));
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
    sensorValueD=(sensorValueD+analogRead(sensorPinD));
   sensorValueD=(sensorValueD+analogRead(sensorPinD));
  sensorValueD=(sensorValueD+analogRead(sensorPinD));
 
of the code,acting as a variance limiting filter, by moving towareds the mean of 6 data points. Some need scaling to reduce size issues of the float string:
 Serial.println((sensorValueB/22));
and for some others we have to filter for 0 values, as the hardware is not as great as the software(the American method over Russian for sensors is good here because the parts are too expensive):

if(sensorValue!=0.00){
But I suppose we are all really just interested in the Graphing. Okay that is next, and connecting to the internet server(and creating it for that matter) will be the topic of another post.

---------------------------------------------------------Graph it------------------------------------------------------------


int a=1; 
 import processing.serial.*;
 
 Serial myPort;        // The serial port
 int xPos = 1;         // graph pos(x)
 
 void setup () {

 size(600, 600);        
 

 println(Serial.list());
 // ACM1 here
 myPort = new Serial(this, Serial.list()[0], 9600);

 myPort.bufferUntil('\n');

 background(255);
 }
 void draw () {

 }
 
 void serialEvent (Serial myPort) {
 // get the data
 String inString = myPort.readStringUntil('\n');
 
 if (inString != null) {

 inString = trim(inString);

 float inByte = float(inString); 
 inByte = map(inByte, 0, 1023, 0, height);
 

 stroke(127,34,255);
 line(xPos, height, xPos, height - inByte);
 
 // start a new picture
 if (xPos >= width) {
  //save(a+".png");
 a=a+1;
 xPos = 0;
 background(0); 

 } 
 else {
 // move
 xPos=xPos+2;
 
 }
 }
 } 
 
 
Yeah, well that will do it. McCormack Out.

No comments:

Post a Comment