Pages: [1]   Go Down
Author Topic: Multiple potentiometers out to serial  (Read 532 times)
0 Members and 1 Guest are viewing this topic.
Boston
Offline Offline
Newbie
*
Karma: 0
Posts: 16
A Godlike N00b
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to have 6 potentiometers reading out to serial in a sequence similar to this.

pot1
pot2
pot3
pot4
pot5
pot1
...continuing

I figured out how to get 1 pot to read out but im having problems getting them all to read out in that order. Below is what im using for the code. If you have any suggestions as to what im doing wrong please share.


const int pot1 = A0;
const int pot2 = A1;
const int pot3 = A2;
const int pot4 = A3;
const int pot5 = A4;

int sensorValue = 0;
int outputValue = 0; 

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

void loop() {

//to print results of pot1

  sensorValue = analogRead(pot1);           
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
         
  Serial.print("pot1" );                     
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);

//to print results of pot2

  sensorValue = analogRead(pot2);           
  outputValue = map(sensorValue, 0, 1023, 0, 255); 

  Serial.print("pot2" );                     
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);

//to print results of pot3

  sensorValue = analogRead(pot3);           
  outputValue = map(sensorValue, 0, 1023, 0, 255); 

  Serial.print("pot3" );                     
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);

//to print results of pot4

  sensorValue = analogRead(pot4);           
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
         
  Serial.print("pot4" );                     
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);

//to print results of pot5

  sensorValue = analogRead(pot5);           
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
         
  Serial.print("pot5" );                     
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 31345
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you post code, select it and hit the # icon.

Quote
but im having problems getting them all to read out in that order
So what order does it output?
It looks like it will continuously output all the pots in order. When it reaches pot5 it will start again with pot1. Your comment says 6 pots but this code will only output five.

You need to learn how to use loops and make the code much much smaller.
Logged

Boston
Offline Offline
Newbie
*
Karma: 0
Posts: 16
A Godlike N00b
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe it was the a baud rate issue cause I was getting random numbers in no particular order. Again very new at this. Do you have any suggestions as to how I could clean up the code? All I really care about is the serial comes out with a label and a value so I know what potentiometer said what value.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 31345
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have any suggestions as to how I could clean up the code?
Well you could try this:-

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

int sensorValue;
void loop() {

//to print results of pot1
  for(int i=0; i<6; i++){
  sensorValue = analogRead(i)>> 4;           
         
  Serial.print("pot"); 
  Serial.print(i);
  Serial.print(" ");   
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);
  }
  Serial.println(" ");
}

Quote
Maybe it was the a baud rate issue cause I was getting random numbers in no particular order.
Do you mean numbers, that is the text bit was printing out fine? If so you haven't connected the pots up correctly.

Quote
All I really care about is the serial comes out with a label
No you don't / should not mean that. You should be keen to find out what you are doing.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I presume you have the outer terminals of each potentiometer connected to +5v and ground, and the wiper connected to an analog input pin?

What value potentiometers are you using? If they are greater than about 47K then they will interfere with each other. To avoid this, each time you do an analogRead of a pot, take 2 readings and discard the first. If you are using high value pots (e.g. 470K or more), you may also need a delay between the 2 readings.
« Last Edit: September 07, 2011, 03:58:12 pm by dc42 » Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: