morelius21 0 Posted October 9, 2016 Share Posted October 9, 2016 Hi i try to read five potentiometer and print this values to serial monitor. I use a for loop but don't work. only work when i use A0, A1,etc.. in analogRead function. Any idea? thanks /* AnalogReadSerial Reads an analog input on pin A0 to A4 (P6.0 to P6.4), prints the result to the serial monitor. Hardware Required: * MSP-EXP430G2 LaunchPad * 10-kilohm Potentiometer * hook-up wire This example code is in the public domain. */ int i = 0; int sensorValue = 0; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // msp430g2231 must use 4800 } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin A0-A4: for (i=0; i < 6; i++){ sensorValue = analogRead(i); Serial.println(sensorValue); delay(300); // delay in between reads for stability } } Quote Link to post Share on other sites
Fmilburn 445 Posted October 9, 2016 Share Posted October 9, 2016 Hi @@morelius21 You are not reading A0, A1, etc. with your code. You are reading pin numbers. See the pin map at http://energia.nu/wordpress/wp-content/uploads/2014/01/MSP430F5529.jpeg In your loop, you are reading pin 0 first (doesn't exist), and then pin 1 (3.3V), etc. What you should be doing is reading from pin 23 through 27. dubnet, energia and morelius21 3 Quote Link to post Share on other sites
enl 227 Posted October 9, 2016 Share Posted October 9, 2016 Starting point is you have an off by one eror on the loop counter: the test should be "<5", not "<6" as you are trying to do it. But, I think the main issue is that A0, A1, etc are aliases for pin identities, not channel values. You might try: const int analogpins[]={A0, A1, A2, A3, A4}; . . . . for (int i=0; i<5; i++) { sensorValue=analogRead(analogpins[i]); . . . morelius21, energia and dubnet 3 Quote Link to post Share on other sites
morelius21 0 Posted October 9, 2016 Author Share Posted October 9, 2016 hi thanks for all replies, @Fmilburn: ok i understand but the samme code in arduino uno work well my code, Maybe is coincidence with the pins coincidence i I was lucky @@enl, you are right is i<5, i will yous your array, i find is the clearest way. now the code work well, thanks! Quote Link to post Share on other sites
B@tto 51 Posted October 13, 2016 Share Posted October 13, 2016 Just a note : if you want to directly select a channel of the ADC, add 128 to the pin value. For example, analogRead(128+5) will return the value of the "real' channel 5 of the ADC. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.