Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumWiring HardwareTroubleshooting › still issues with analogRead? (v15-16)

Page Index Toggle Pages: 1
still issues with analogRead? (v15-16) (Read 10139 times)
05/20/08 at 23:57:02

roypardi   Offline
Junior Member
I Love YaBB 2!
Boston USA

Posts: 57
**
 
Hi - I noted some discussion earlier about issues with analogRead (though my search just now didn't turn them up).

Anyway, I am using 3 4051 multiplexers (not in series) and have some code that worked fine with Arduino but was erratic with Wiring v15 & 16. I tried the same code with Wiring 14 and it worked fine. I plan to try 15 & 16 again tomorrow but I thought I would send up a flare to see if there were any known issues with analogRead.

--Roy

Code:
/*
routine to read 24 sensor states and store in array
 */
int rangeLimit = 25;
int sensorStates[24];
//analog pins
int analogPins[3] = {
  0,1,2};
int multiplexerPins[3][3] = {
  {
    24, 25, 26    }
  , {
    27, 28, 29    }
  , {
    35, 36, 37    }
};


int r0 = 0;	//value select pin at the 4051 (s0)
int r1 = 0;	//value select pin at the 4051 (s1)
int r2 = 0;	//value select pin at the 4051 (s2)
int row = 0;     // storeing the bin code
int count = 0;    // just a count

int  bin [] = {
  000, 1, 10, 11, 100, 101, 110, 111};

int sensorValue;
int range;
int LED = 48;


void setup(){
  pinMode(LED, OUTPUT);

  int i;
  for (i=0;i<3;i++){
    pinMode(multiplexerPins[i][0], OUTPUT);
    pinMode(multiplexerPins[i][1], OUTPUT);
    pinMode(multiplexerPins[i][2], OUTPUT);
  }

  for (i = 0; i < 24; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }

  Serial.begin(9600);

  for (count=0; count<3; count++) {
    digitalWrite(LED, HIGH);
    delay(100);
    digitalWrite(LED, LOW);
    delay(50);
  }
}



void loop () {

 updateSensorStates();
 displayLights();
}

void displayLights(){

  for (int i = 0;i<24;i++){
    digitalWrite(i, sensorStates[i]);
  }
}

void updateSensorStates(){
   int i;
  for(i=0;i<3;i++){
    for (count=0; count<8; count++) {
	row = bin[count];
	r0 = row & 0x01;
	r1 = (row>>1) & 0x01;
	r2 = (row>>2) & 0x01;
	digitalWrite(multiplexerPins[i][0], r0);
	digitalWrite(multiplexerPins[i][1], r1);
	digitalWrite(multiplexerPins[i][2], r2);
	sensorValue = analogRead(analogPins[i]);

	if (sensorValue> 3) {
	  range = (6787 / (sensorValue - 3)) - 4;
	  //high/1 = OFF for AC boards
	  if (range > rangeLimit){
	    range = 1;
	  }
	  else {
	    range = 0;
	  }
	}
	else {
	  range = 1;
	}
	sensorStates[i * 8 + count] = range;
    }
  }
} 

 
IP Logged
 
Reply #1 - 05/21/08 at 01:46:54

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
I bet the Wiring board is running faster then the Arduino board and you need to
let the multiplexors settle before doing the analogRead().

Try adding a delay before the read and see how it goes.
 
IP Logged
 
Reply #2 - 05/21/08 at 11:47:14

Titus   Offline
Full Member
Wiring Soccer Robot!
Amsterdam

Posts: 176
***
 
did you download wiring-0016 Again? Some error was fixed down the pulse in commands, perhaps the same error?
Try downloading 0016 again.

Titus
 

Check www.TeamVossius.nl for our Autonomous soccer robots!
IP Logged
 
Reply #3 - 05/22/08 at 00:56:06

roypardi   Offline
Junior Member
I Love YaBB 2!
Boston USA

Posts: 57
**
 
So I redownloaded v16 today and tried my code with it. Still seeing anomalies- basically the pins assignments are all shifted by 1. The same code works as expected in v14.

I don't have a barebones example - the project is using 24 sensors and 6 AC controller boards (each with 4 AC outs). The gist of the error is that with v16, sensor n turns on the pin n + 1 to the AC board. In v14, sensor n turns on pin n.

I thought the issue was with analogRead but maybe it is something else (if there is an issue - but the same code performs differently between v14 + v15/16)

When I clear this project deadline I will try to come up with a clear test case.

--Roy
 
IP Logged
 
Reply #4 - 05/22/08 at 02:37:32

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
Did you try a 10 millisecond delay before the analogRead()?

If you switch the mux and then read, you're going to get the
previous voltage. You need to let the analog-input settle for
a little bit before reading it.
 
IP Logged
 
Reply #5 - 05/22/08 at 04:08:24

roypardi   Offline
Junior Member
I Love YaBB 2!
Boston USA

Posts: 57
**
 
Hi - I don't think that is it; at least with v14 I am able to read all 24 inputs (from 3 4051 mux chips using 1 analog pin each) in 10ms and the readings are accurate. I don't know where the issue is at this point but will try to find some time to break out a test case.
--Roy
 
IP Logged
 
Reply #6 - 05/22/08 at 06:13:56

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
hi, which version is giving you good readings?
The only recent change in the analog section was removing an average of 3 readings before returning the result. Now it is making one reading and returning the result. Have you tried to test the analogRead with a potentiometer?

 
IP Logged
 
Reply #7 - 05/22/08 at 14:12:53

roypardi   Offline
Junior Member
I Love YaBB 2!
Boston USA

Posts: 57
**
 
v14 is the one that is working for me. I'm on an exhibit deadline so haven't had the time to scope things out.

The set up is 3 4051 chips attached to Analogin 0-2 and the control lines attached to pins 24-29, 35-37. I am using the GP2D12 sensor (24) powered separately (common gnd). There are also 6 AC controller boards attached to pins 0-23. The barebones functionality is: trigger one of the sensors and a corresponding AC pin goes high.

I was trying it with v16 and getting odd results. After checking/fixing my hardware set up I couldn't think of what else to try so I tried v15 and then v14. With v14 it worked as expected.

Which version had the analogIn change from 255 to 1024?

Anyway - I'll try both v15 & 16 again today to see if there are any more clues.
 
IP Logged
 
Reply #8 - 05/22/08 at 21:43:36

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
hi, analogWrite changed from 255 to 1023, analogRead was always in the 0-1023 range. What changed for 15 is this:
It used to be that everytime an analogRead command was issued, the system made 3 readings and average and averaga the readings (read1+read2+read3)/3 and then a value was returned. Now there is no average just a value returned. Removing the average increased the speed for reading an analog input.
Have you tried to read a simple potentiometer?
 
IP Logged
 
Reply #9 - 05/23/08 at 00:25:41

roypardi   Offline
Junior Member
I Love YaBB 2!
Boston USA

Posts: 57
**
 
haven't tried a pot yet since v14 is working for me. I did test v16 again today and there were two things odd

1. sensors that work fine in v14 were firing on their own
2. the pin assignments I set up to respond to sensor firing are off by 1 (higher) - so sensor 2 is causing digital pin 3 to go high.

#2 is odd - and I would readily think it is my code - except it works solidly in v14. Have no other info or clues.
 
IP Logged
 
Reply #10 - 05/23/08 at 13:35:16

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
delay(10); analogRead();
 
IP Logged
 
Page Index Toggle Pages: 1