Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIC32-PINGUINO-OTG analogRead problems
05-11-2011, 11:16 PM,
#1
PIC32-PINGUINO-OTG analogRead problems
Hello!
I'm trying to create a program that would read 6 analog inputs and print them to a USB console. The problem is I get mixed results, some of the inputs get read, some seem to produce out of bounds values. Also the LEDs don't work if I set pinMode(X, INPUT) on analog pins A3-A5.

Here is the program:
Code:
int LED_I = 0;      // orange
int LED_Z = 13;     // green
int LED_RD = 3;    // red
int LED_GR = 4;    // green
int BUT = 2;      // button

int    value[6];
int inputs[6];

/*
    To use, run:
    minicom -D /dev/ttyACM0
*/

void setup() {
    pinMode(LED_I, OUTPUT);
    pinMode(LED_Z, OUTPUT);
    pinMode(LED_RD, OUTPUT);
    pinMode(LED_GR, OUTPUT);
    
    pinMode(BUT, INPUT);
    
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);
    pinMode(A3, INPUT);
    pinMode(A4, INPUT);
    pinMode(A5, INPUT);
    
    inputs[0] = A0;
    inputs[1] = A1;
    inputs[2] = A2;
    inputs[3] = A3;
    inputs[4] = A4;
    inputs[5] = A5;
}

loop() {

    digitalWrite(LED_RD, HIGH);
    digitalWrite(LED_GR, HIGH);
    delay(300);
    digitalWrite(LED_RD, LOW);
    digitalWrite(LED_GR, LOW);
    delay(100);
    digitalWrite(LED_GR, HIGH);

    // wait for RETURN key to start
    CDC.println("Press RETURN to start.");
    while (CDC.getKey() != '\r');
//    CDC.println(" ");
        
    digitalWrite(LED_GR, LOW);
    digitalWrite(LED_RD, HIGH);
    
    while (digitalRead(BUT)) {
        int i;
        for (i = 0; i < 6; i++) {
            value[i] = analogRead(inputs[i]);
        }

        CDC.println("%d %d %d %d %d %d", value[0], value[1], value[2], value[3], value[4], value[5]);
        delay(10);
    }
    digitalWrite(LED_RD, LOW);
}

The output I get is:
Quote:Press RETURN to start.
1021 373 373 -1610611752 -1660906916 1048579
1020 582 582 -1610611752 -1660906916 1048579
1018 386 386 -1610611752 -1660906916 1048579
1020 553 553 -1610611752 -1660906916 1048579
...
Reply
08-11-2011, 06:34 PM,
#2
RE: PIC32-PINGUINO-OTG analogRead problems
Hi Alfred

You are right, I gave a try and it seems I have the same problem here.
I am working on a fix for the analog module.
As soon as it will be ready, I tell you where to download the patch.

Jean-Pierre

(05-11-2011, 11:16 PM)Alfred Wrote: Hello!
I'm trying to create a program that would read 6 analog inputs and print them to a USB console. The problem is I get mixed results, some of the inputs get read, some seem to produce out of bounds values. Also the LEDs don't work if I set pinMode(X, INPUT) on analog pins A3-A5.

Here is the program:
Code:
int LED_I = 0;      // orange
int LED_Z = 13;     // green
int LED_RD = 3;    // red
int LED_GR = 4;    // green
int BUT = 2;      // button

int    value[6];
int inputs[6];

/*
    To use, run:
    minicom -D /dev/ttyACM0
*/

void setup() {
    pinMode(LED_I, OUTPUT);
    pinMode(LED_Z, OUTPUT);
    pinMode(LED_RD, OUTPUT);
    pinMode(LED_GR, OUTPUT);
    
    pinMode(BUT, INPUT);
    
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);
    pinMode(A3, INPUT);
    pinMode(A4, INPUT);
    pinMode(A5, INPUT);
    
    inputs[0] = A0;
    inputs[1] = A1;
    inputs[2] = A2;
    inputs[3] = A3;
    inputs[4] = A4;
    inputs[5] = A5;
}

loop() {

    digitalWrite(LED_RD, HIGH);
    digitalWrite(LED_GR, HIGH);
    delay(300);
    digitalWrite(LED_RD, LOW);
    digitalWrite(LED_GR, LOW);
    delay(100);
    digitalWrite(LED_GR, HIGH);

    // wait for RETURN key to start
    CDC.println("Press RETURN to start.");
    while (CDC.getKey() != '\r');
//    CDC.println(" ");
        
    digitalWrite(LED_GR, LOW);
    digitalWrite(LED_RD, HIGH);
    
    while (digitalRead(BUT)) {
        int i;
        for (i = 0; i < 6; i++) {
            value[i] = analogRead(inputs[i]);
        }

        CDC.println("%d %d %d %d %d %d", value[0], value[1], value[2], value[3], value[4], value[5]);
        delay(10);
    }
    digitalWrite(LED_RD, LOW);
}

The output I get is:
Quote:Press RETURN to start.
1021 373 373 -1610611752 -1660906916 1048579
1020 582 582 -1610611752 -1660906916 1048579
1018 386 386 -1610611752 -1660906916 1048579
1020 553 553 -1610611752 -1660906916 1048579
...

Reply
08-11-2011, 06:54 PM,
#3
RE: PIC32-PINGUINO-OTG analogRead problems
Thanks for looking into this. When I get to know the C code parts a little better maybe I can figure those things out myself. Still, hardware programming is like magic to me right now. Wink

PS I had a friend test this program on his Arduino and it seems to work fine, apart from the LEDs, because he did not have any.
Reply
08-11-2011, 07:16 PM,
#4
RE: PIC32-PINGUINO-OTG analogRead problems
Hi Alfred

I updated the analog.c file in the svn revision ( r224 ). If you want, you can just change the file in the p32/include/pinguino/core or p32/include/pinguino/basic ( depending of the version you are currently using ). I joined the new file to this email.

I tested the new analog library with this small program:
int i;
int value;

void setup()
{
Serial.begin(9600);
}

void loop()
{
for (i=0;i<8;i++)
{
value=analogRead(i);
Serial.printf("A%d=%d ",i,value);
}
Serial.printf("\n\r");
delay(100);
}

We keep in touch.
Jean-Pierre

(08-11-2011, 06:54 PM)Alfred Wrote: Thanks for looking into this. When I get to know the C code parts a little better maybe I can figure those things out myself. Still, hardware programming is like magic to me right now. Wink

PS I had a friend test this program on his Arduino and it seems to work fine, apart from the LEDs, because he did not have any.



Attached Files
.zip   analog.c.zip (Size: 1.47 KB / Downloads: 14)
Reply
08-11-2011, 08:11 PM,
#5
RE: PIC32-PINGUINO-OTG analogRead problems
Thank you! This seems to have solved the problems, all the pins seem to get acceptable values now, and the LEDs work fine too!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)