I've been doing an extra experiment for an electronics class on Analogue to Digital to conversion.
The conversion is flash type. The priority encoder was built out of logic gates. And basically my question is where would I begin in order to have the binary outputs from my encoder circuit inputted into my arduino to have them converted to decimal and displayed on the computer?
My idea is that I would have to write each condition in for each binary sequence (0-4), but where I get stuck is would I need to then use processing to have them displayed on the computer?
The Arduino will act like a serial port device so anything you Serial.print() from the Arduino can be read by your program listening on the serial port.
Thanks for that post that gave me an idea of what I had to be doing.
However I have a problem,
I wrote the code in the arduino and used its serial monitor function and it was working correctly.
When I tried making a code for processing nothing really happened it gave me a grey box and constantly printed a string of 0's.
What I'd like to see happen is a single number change as light levels increased as apposed to a long string of numbers.
At processing;
import processing.serial.*;
Serial myPort;
int val;
void setup()
{
size(200, 200);
println(Serial.list());
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw()
{
val = myPort.read(); // read it and store it in val
if (val == 1) {
print('1');
}
else if (val == 2) {
print('2');
} else if (val == 3) {
print('3');
} else {
print('0');
}}
At the Arduino:
//Priority encoder to Computer code//
int onepin = 2; //digital input pins//
int twopin = 3;
int fourpin = 4;
void setup() //Setup //
{
pinMode(onepin, INPUT);
pinMode(twopin, INPUT);
pinMode(fourpin, INPUT);
Serial.begin(9600);
}
void loop()
{
if( digitalRead(onepin)== HIGH && digitalRead(twopin)== LOW && digitalRead(fourpin)== LOW)
{
Serial.write('1');
delay(100);
}
else if ( digitalRead(onepin)== LOW && digitalRead(twopin)== HIGH && digitalRead(fourpin)== LOW)
{
Serial.write('2');
delay(100);
}
else if ( digitalRead(onepin)== HIGH && digitalRead(twopin)== HIGH && digitalRead(fourpin)== LOW)
{
Serial.write('3');
delay(100);
}
else if ( digitalRead(onepin)== LOW && digitalRead(twopin)== LOW && digitalRead(fourpin)== HIGH)
{
Serial.write('4');
delay(100);
}
else
{
Serial.write('0');
delay(100);
}}
Firstly, in the arduino sketch you're printing the ascii characters '1', '2' etc instead of the numbers 1, 2, etc. This is why the Processing sketch doesn't recognise your input. If you wrote the numbers instead, like this, I think you'll have more success:
Serial.write(1);
Secondly, you can combine the input states into your number in a much simpler and easier way. Just use one bit per input so that B00000001 means your input states are LOW LOW LOW HIGH, B00000010 means your input states are LOW LOW HIGH LOW and so on. On the Arduino side you can implement this very easily by writing a for loop to perform a digitalRead() on each pin and use bitWrite() to write the result to the result which you will eventually write to the serial port. That's much cleaner than explicitly testing for each possible combination of input states.
I've done some research and was able to get processing to read the com port, I found and modified the following code from a blog I found how ever I have forgotten the address. The problem with this is that it acts like a magnified serial monitor. How would I go about having a non-scrolling text with just a variable changing? (example Light Level: 'x')
import processing.serial.*;
String lightlevel = "0";
PFont font;
Serial port;
void setup() {
String portName = Serial.list()[0];
port = new Serial(this, portName, 9600);
font = loadFont("Calibri-36.vlw");
textFont(font);
textAlign(CENTER);
size(200, 140);
background(0);
fill(0);
smooth();
}
void draw() {
if (port.available()>0) {
delay(100);
lightlevel = port.readString();
}
background(255); // Set background to dark gray
text(lightlevel, width/2, height/2);
}
As for the arduino side, what peterH pointed out about the ascii characters helped so thank you for that. However I still have not tried what he has further suggested about the input states being simplified. I'll try to get to that later, I'm not exactly sure how I would write it and the reference app doesn't give much detail on it. For now I'm just leaving it as is for demonstration purposes.
int onepin = 2; //digital input pins//
int twopin = 3;
int fourpin = 4;
int val;
void setup() //Setup //
{
pinMode(onepin, INPUT);
pinMode(twopin, INPUT);
pinMode(fourpin, INPUT);
Serial.begin(9600);
}
void loop()
{
if( digitalRead(onepin)== HIGH && digitalRead(twopin)== LOW && digitalRead(fourpin)== LOW)
{
val = 1;
delay(100);
}
else if ( digitalRead(onepin)== LOW && digitalRead(twopin)== HIGH && digitalRead(fourpin)== LOW)
{
val = 2;
delay(100);
}
else if ( digitalRead(onepin)== HIGH && digitalRead(twopin)== HIGH && digitalRead(fourpin)== LOW)
{
val = 3;
delay(100);
}
else if ( digitalRead(onepin)== LOW && digitalRead(twopin)== LOW && digitalRead(fourpin)== HIGH)
{
val = 4;
delay(100);
}
else
{
val = 0;
delay(100);
}
Serial.println(val);
}
void loop() {
val = 0; // initialize to zero
if( digitalRead(onepin)== HIGH)
val &= B00000001;
if (digitalRead(twopin) == HIGH)
val &= B00000010;
if (digitalRead(fourpin) == HIGH)
val &= B00000100;
delay(100);
Serial.println(val);
}
if( digitalRead(onepin)== HIGH)
val &= B00000001;
if (digitalRead(twopin) == HIGH)
val &= B00000010;
if (digitalRead(fourpin) == HIGH)
val &= B00000100;
delay(100);
Serial.println(val);
}
That seems to be clearing the other bits rather than setting the bit you wanted. In any case I'd suggest putting the pin numbers in an array and using a for loop to read from each pin and write the result to the corresponding bit using bitWrite().
I was able to get to work however I had the strange occurrence of it going to 0 when the analogue side went to level 4. It switch back and forth between 0 and 4.
Anyways it was working and if your interested in seeing what I combined to make this circuit happen here it is.
//Priority encoder to Computer code//
//Alexander W. Abernethy//
int onepin = 2; //digital input pins//
int twopin = 3;
int fourpin = 4;
int val;
void setup() //Setup //
{
pinMode(onepin, INPUT);
pinMode(twopin, INPUT);
pinMode(fourpin, INPUT);
Serial.begin(9600);
}
void loop()
{
val = 0; // initialize to zero
if( digitalRead(onepin)== HIGH)
val = B00000001;
if (digitalRead(twopin) == HIGH)
val = B00000010;
if (digitalRead(twopin) == HIGH && digitalRead(onepin)==HIGH)
val = B00000011;
if (digitalRead(fourpin) == HIGH)
val = B00000100;
delay(100);
Serial.println(val);
}
if( digitalRead(onepin)== HIGH)
val = B00000001;
if (digitalRead(twopin) == HIGH)
val = B00000010;
if (digitalRead(twopin) == HIGH && digitalRead(onepin)==HIGH)
val = B00000011;
if (digitalRead(fourpin) == HIGH)
val = B00000100;
void loop()
{
val = 0; // initialize to zero
for(n=4; n<1; n--){ //Most Significant Bit first, Least Significant Bit last
val = (val * 2) + digitalRead(n) //*2 shifts all bits left by one position, LSB will be 0. +digitalRead adds 1 or 0 to the LSB Do 3 times and each bit will be in the correct position.
}