Pages: [1]   Go Down
Author Topic: Arduino Uno: How would I input a priority encoder?  (Read 1054 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2546
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use any language: http://playground.arduino.cc/Main/InterfacingWithSoftware

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
Code:
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:
Code:
//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);
  }}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can see two issues.

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:

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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')

Code:
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.

Code:
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);
}
Logged

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2546
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See http://playground.arduino.cc/Code/BitMath

Code:
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);
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See http://playground.arduino.cc/Code/BitMath

Code:
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);
}


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().
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2546
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoops. Those "&=" should be "|=".

I assure you this is the first time I've ever made a programming mistake.
Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1492
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you assign your inputs to three adjacent pins, say 6, 7 and 8, with 6 being the fourpin and 8 the onepin then:
Code:
void loop(){
   val = 0;
   for(n=6; n<9; n++){  //most significant bit first, least significant bit last
      val = (val * 2) + digitalRead(n)
   }
}
« Last Edit: March 22, 2013, 11:07:56 am by Henry_Best » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Page 84 of this learning lab manual for Radio Shack. (analogue side)
http://www.radioshack.com/graphics/uc/rsk/Support/ProductManuals/2800027_P1_PM_EN.pdf

The priority encoder described in this youtube video ( analogue to digital side.)


Modified code from this link (procressing side) smiley-lol:
http://arduinobasics.blogspot.com/2011/06/displaying-serial-data-from-arduino-uno.html

Code:
import processing.serial.*;
Serial myPort;
String sensorReading="";
PFont font;


void setup() {
  size(400,200);
  myPort = new Serial(this, "COM3", 9600);
  myPort.bufferUntil('\n');
  font = createFont(PFont.list()[2],32);
  textFont(font);
}

void draw() {
  //The serialEvent controls the display


void serialEvent (Serial myPort){
 sensorReading = myPort.readStringUntil('\n');
  if(sensorReading != null){
    sensorReading=trim(sensorReading);
  }

  writeText("Light Intensity Level: " + sensorReading);
}


void writeText(String textToWrite){
  background(255);
  fill(0);
  text(textToWrite, width/20, height/2);   
}

And here is the still bulky arduino code:

Code:
//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);
}
 






Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1492
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


And here is the still bulky arduino code:

Code:
//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);
}
 

Try this:
Code:

int n;
int val;

void setup() //Setup //
{
  pinMode(2, INPUT); //LSB
  pinMode(3, INPUT);
  pinMode(4, INPUT); //MSB
 
  Serial.begin(9600);
}

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.
  }

  delay(100);

  Serial.println(val);
}





Logged

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2546
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you want (n = 4; n > 1; n--)
Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1492
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you want (n = 4; n > 1; n--)

Yes. You think correctly. My bad.
Logged

Pages: [1]   Go Up
Jump to: