Jump to content
43oh

some pins get HIGH when i analogRead(A10)


Recommended Posts

Since I'm having trouble getting my serial to work, i'm thinking of other ways to probe the values from a sensor i have...

 

so i attached a few leds to P1_0 to P1_5 and light them up based on the values i'm getting so i can probe around and find the range the sensor work.

 

before i hook up the sensor i tried to test it out with the internal temperature sensor and found this oddity... it may very well be something extremely dumb with my code...

 

 

/* 

attach leds and resistors on pins 1.0 to 1.5

*/

// heart beat
int ledState = LOW;
long previousMillis = 0;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
 // set the digital pin as output:
 pinMode(GREEN_LED, OUTPUT); // green led for heart beat
 // segment leds
 pinMode(P1_0, OUTPUT);
 pinMode(P1_1, OUTPUT);
 pinMode(P1_2, OUTPUT);
 pinMode(P1_3, OUTPUT);
 pinMode(P1_4, OUTPUT);
 pinMode(P1_5, OUTPUT);
 // input for sensor
 pinMode(P2_6, INPUT);
 // internal thermometer
 //pinMode(A10, INPUT); //need to initialize?

 // test the lights
 light(0, 1000); // 0
 delay(300);
 light(1, 1000); // 1
 delay(300);
 light(167, 1000); // 2
 delay(300);
 light(334, 1000); // 3
 delay(300);
 light(501, 1000); // 4
 delay(300);
 light(667, 1000); // 5
 delay(300);
 light(834, 1000); // 6
 delay(300);

 // now blink three times and lights out.
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);

}

int x;

void loop(){
 x = analogRead(A10);
 light(x, 120);
 //x = analogRead(P2_6);


 // heartbeat
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;// save the last time you blinked the LED
   ledState = !ledState;
   digitalWrite(GREEN_LED, ledState);
 }
}

void light( int val, int ceiling ){
 int increments = ceiling/6;
 if( val > (increments*5) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, HIGH);
     digitalWrite(P1_2, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, HIGH);
   }else if( val > (increments*4) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, HIGH);
     digitalWrite(P1_2, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, LOW);
   }else if( val > (increments*3) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, HIGH);
     digitalWrite(P1_2, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
   }else if( val > (increments*2) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, HIGH);
     digitalWrite(P1_2, HIGH);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
   }else if( val > increments ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, HIGH);
     digitalWrite(P1_2, LOW);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
   }else if( val > 0 ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_1, LOW);
     digitalWrite(P1_2, LOW);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
   }else{
     digitalWrite(P1_0, LOW);
     digitalWrite(P1_1, LOW);
     digitalWrite(P1_2, LOW);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);      
   }
}

 

it have the following features:

- green led will blink every 1s to show it's still alive.

- at power up, 6leds will light up one at a time, and then flash 3x. (mostly to unit test my code)

- after that, will analogRead(A10) every cycle and every second update the leds.

 

what i expect this does, is to every second to light up the leds based on the temperature.

 

what happens...

- green led blinks away every second just fine

- at power up they light up as expected.

- leds on P1_0, P1_2, P1_4, P1_5 stays ON at all times

- leds on P1_1, P1_3 stays OFF at all times.

 

any ideas?

 

update:

tried to move the update probe to the heartbeat condition (and same thing still happens), so this rule out that the analogRead(A10) is setting the pins on HIGH as i was thinking...

 

update2:

if I simply remove the code in my light() function, when analogRead(A10) happens i can see the led in P1_1 almost lighted on... (i'm using a 220Ohm on all leds, they are 20mA, ~2Vf)

 

I see that same dimmed light on the leds when i press RESET (on P1_1 and P1_2)

Link to post
Share on other sites

Now that i got serial to work (linux driver issue), i had to move those led pins around... now they are on

P1_0 and then P1_3 to P2_1

 

but still, now i have:

 x = analogRead(A10);
 light(x, 120);
 Serial.println(x);

 

as soon as i run the 1st line here, LED connected to 1.3 goes dark (or dimmed... can't really tell as i'm using a 10segment display and there's some light bleeding from the others)

 

serial is working fine, even though the code before having serial dimmed the leds on TX,RX, now it seems to have no effect on serial communications.

 

on the serial line i keep getting a number around 327, that slowly climbs to 333 and stays there. or climbs rapidly if i reset it instead of leaving it offline for some time.

Link to post
Share on other sites

in pins_energia.h A10 and P2_2 equal 10. Does that means analogRead is definitely not the way to read the

temp sensor?

 

/* 

attach leds and resistors on pins 1.0 , 1.3 to 2.1

*/

// heart beat
int ledState = LOW;
long previousMillis = 0;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() { 
 // set the digital pin as output:
 pinMode(GREEN_LED, OUTPUT); // green led for heart beat
 // segment leds
 pinMode(P1_0, OUTPUT);
 pinMode(P1_3, OUTPUT);
 pinMode(P1_4, OUTPUT);
 pinMode(P1_5, OUTPUT);
 pinMode(P2_0, OUTPUT);
 pinMode(P2_1, OUTPUT);

 // test the lights
 light(0, 1000); // 0
 delay(300);
 light(1, 1000); // 1
 delay(300);
 light(167, 1000); // 2
 delay(300);
 light(334, 1000); // 3
 delay(300);
 light(501, 1000); // 4
 delay(300);
 light(667, 1000); // 5
 delay(300);
 light(834, 1000); // 6
 delay(300);

 // now blink three times and lights out.
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);
 light(834, 1000);
 delay(300);
 light(0, 1000);
 delay(300);

 // start internal temp sensor
 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON;
 ADC10CTL1 = INCH_10 + ADC10DIV_3;   // Channel 10 = Tem

 // open serial line
 Serial.begin(9600);
}

int x;

void loop(){
 // heartbeat
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;// save the last time you blinked the LED
   ledState = !ledState;
   digitalWrite(GREEN_LED, ledState);

//  x = analogRead(A10);

   ADC10CTL0 |= ENC + ADC10SC;
   while (!(ADC10CTL0 & ADC10IFG))
       /* wait until conversion is completed */ ;

   unsigned adc = ADC10MEM;
   // shut off conversion and lower flag to save power.
   // ADC10SC is reset automatically.
   while (ADC10CTL0 & ADC10BUSY)
       /* wait for non-busy per section 22.2.6.6 in User's Guide */ ;

   ADC10CTL0 &= ~ENC;
   ADC10CTL0 &= ~ADC10IFG;
   // ADC to C degrees
   unsigned c = (adc * 27069L - 18169625L) >> 16;
   light(c, 80); // light leds in a scale of 0C to 80C
   Serial.println(c);
 }
}

void light( int val, int ceiling ){
 int increments = ceiling/6;
 if( val > (increments*5) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, HIGH);
     digitalWrite(P2_0, HIGH);
     digitalWrite(P2_1, HIGH);
   }else if( val > (increments*4) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, HIGH);
     digitalWrite(P2_0, HIGH);
     digitalWrite(P2_1, LOW);
   }else if( val > (increments*3) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, HIGH);
     digitalWrite(P2_0, LOW);
     digitalWrite(P2_1, LOW);
   }else if( val > (increments*2) ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, HIGH);
     digitalWrite(P1_5, LOW);
     digitalWrite(P2_0, LOW);
     digitalWrite(P2_1, LOW);
   }else if( val > increments ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, HIGH);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
     digitalWrite(P2_0, LOW);
     digitalWrite(P2_1, LOW);
   }else if( val > 0 ){
     digitalWrite(P1_0, HIGH);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
     digitalWrite(P2_0, LOW);
     digitalWrite(P2_1, LOW);
   }else{
     digitalWrite(P1_0, LOW);
     digitalWrite(P1_3, LOW);
     digitalWrite(P1_4, LOW);
     digitalWrite(P1_5, LOW);
     digitalWrite(P2_0, LOW);
     digitalWrite(P2_1, LOW);
   }
}

 

most temp sensor code from here http://www.cashdollar.org/2012/07/26/si ... onversion/

Link to post
Share on other sites

Happy to hear that you got Serial to work and thank you for trying so hard to make this work. Would you mind posting what you had to do to get it to work on Linux?

 

analogRead() is done on channels rather than the pin. Channels map to pins.

 

A10 is special and does not have a pin to go with it since it is the internal temperature sensor. A0 Would be pin 2 aka P1_0.

 

See https://github.com/energia/Energia/wiki/Hardware.

 

static const uint8_t A0  = 0; 
static const uint8_t A1  = 1; 
static const uint8_t A2  = 2; 
static const uint8_t A3  = 3; 
static const uint8_t A4  = 4; 
static const uint8_t A5  = 5; 
static const uint8_t A6  = 6; 
static const uint8_t A7  = 7;
static const uint8_t A10 = 10; // special. This is the internal temp sensor

 

I'll have to look at your code in a bit more detail later today to see what is going on.

Link to post
Share on other sites
Happy to hear that you got Serial to work and thank you for trying so hard to make this work. Would you mind posting what you had to do to get it to work on Linux?

 

sure, did that one the other thread... viewtopic.php?f=38&t=3370

olivluca sent me to the wiki page with the linux driver fix (afraid to call that a fix since it will give problems for other use cases down the road, but hey, i get data and i couldn't do better anyway :)

 

maybe need to link that wiki page from the main serial page? i thought i had gone trhu all wiki pages until olivluca sent me that link.

 

analogRead() is done on channels rather than the pin. Channels map to pins.

 

A10 is special and does not have a pin to go with it since it is the internal temperature sensor. A0 Would be pin 2 aka P1_0.

 

See https://github.com/energia/Energia/wiki/Hardware.

 

static const uint8_t A0  = 0; 
static const uint8_t A1  = 1; 
static const uint8_t A2  = 2; 
static const uint8_t A3  = 3; 
static const uint8_t A4  = 4; 
static const uint8_t A5  = 5; 
static const uint8_t A6  = 6; 
static const uint8_t A7  = 7;
static const uint8_t A10 = 10; // special. This is the internal temp sensor

 

I'll have to look at your code in a bit more detail later today to see what is going on.

 

I think that a generic example on how to probe A10 would sufice for me to understand it.

 

The page you link to is pretty much my new wallpaper :) but it does not mention anything about threating analogs reads differently than pins. Since in arduino it's not so different i thought it would apply here in Energia as well.

 

Also, that wiki page:

- mentions GREEN_LED; PUSH2; TEMPSENSOR; as all the same.

- has no analogRead examples.

 

I can't say i know the intended Energia way to use analogRead...

Link to post
Share on other sites

There is an example for reading the internal temp sensor in the Energia examples repository: https://github.com/energia/Energia/blob ... er_430.ino

 

Can you explain what you mean with "mentions GREEN_LED; PUSH2; TEMPSENSOR; as all the same.". These are associated with different pins. We might have done a poor job in explaining this so your input would be helpful.

 

As for the Ax inputs. A0 is channel 0 of the ADC which happens to be on PIN 2 of the LaunchPad (P1.0 of the chip). So there really is no relation between the LaunchPad pin number Ax.

 

Look at it this way. A0 is channel 0 of the ADC which happens to be PIN 2 of the LaunchPad. If I could redo the silk screen then the silk screen would read A0 just as on the Arduino boards.

 

Hope this makes it a bit clearer.

 

Robert

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...