reading a ac signal

edited August 2014 in Arduino

the plan is to read one half of the ac signal threw a0 on the arduino and the other half of the signal threw a1. in the processing software I have got positive part of the signal where I want it. but im not sure how to invert the negative part of the signal in software.

 import processing.serial.*;

 Serial myPort;        // The serial port
 int xPos = 1;         // horizontal position of the graph
 float inByte1= 0;
 PFont font;
 void setup () {
 // set the window size:
 size(800, 700);        
 textFont(createFont("bold", 20));  
 // List all the available serial ports
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my  Arduino, so I open Serial.list()[0].
 // Open whatever port is the one you're using.
 myPort = new Serial(this,"COM4", 18200);
 // don't generate a serialEvent() unless you get a newline character:
 myPort.bufferUntil('\n');
 // set inital background:
 background(0);
 }
 void draw () {
 // everything happens in the serialEvent()
 }

 void serialEvent (Serial myPort) {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');

 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // convert to an int and map to the screen height:
 float inByte = float(inString); 
 inByte = map(inByte, 0, 695, 0, height);

 // draw the line:         // color changes with voltage
 if (inByte>600) {
 stroke(227,234,25);
 }
 if (inByte>500 &inByte<600) {
 stroke(227,134,25);
 }
 if (inByte>400 &inByte<500) {
 stroke(227,134,225);
 }
 if (inByte>300 & inByte<400) {
 stroke(17,234,255);
 }
 if (inByte>200 &inByte<300) {
 stroke(27,134,225);
 }
 if (inByte>100 &inByte<200) {
 stroke(227,134,0);
 }
  if (inByte<100) {
 stroke(127,34,255);
 }
 line(xPos, height/2, xPos, height/2 - inByte);puts in the middle of the screen
 fill(120,110,90); 

stroke(255); 

rect(10, 50, 90, 60); 
fill(0);
inByte1 = inByte/1024*5.0;//convert to a voltage
inByte1 = inByte1*4.35;//use if using the voltage divider
text(inByte1, 20, 105); 

 // at the edge of the screen, go back to the beginning:
 if (xPos >= width) {
 xPos = 0;
 background(0); 
 } 
 else {
 // increment the horizontal position:
 xPos++;

 }
 }
 }

Answers

  • Hello ! What is the question exactly ? Can you give an exemple of what values you get and what values you want ? Because even after reading your code with attention, I didn't figure out what you want to do :)

  • inByte = map(inByte, 0, 695, 0, height); makes the signal match the height of the screen line(xPos, height/2, xPos, height/2 - inByte);puts the signal in the middle of the screen. from here I want to make the middle of the screen the zero reference point. from here add the negative portion of the signal to the screen

  • Answer ✓

    I'm not totally sure to understand (english is not my native language) but

    line(xPos, height/2, xPos, height/2 + inByte);

    doesn't work ?

  • got it figured out already. yes your answer is correct.

Sign In or Register to comment.