Small project/ expanding starter kit

hi , so far i haven't found a very strait forward answer to this, for beginners

Project 5 in the starter kit .. servo and pot

lets say you want to expand this .. 2xservo 2xpot and so on... etc ...

wiring this seems easy

but coding ,, ? how would you adapt this given example to an extra input and output, say , pwm10(servo)
analog 1 (pot)

p05_ServoMoodIndicator.ino (1.25 KB)

Read the sticky at the top of the forum.

Mark

Adamkdrums:
but coding ,, ? how would you adapt this given example to an extra input and output, say , pwm10(servo)
analog 1 (pot)

Have you tried dupilcating the code in loop() but using the appropriate pin numbers for the new components. And you don't need to duplicate the delay(15) - just keep that as the last thing in loop().

The Arduino is a great system for learning-by-doing.

...R

erm, kida, wouldnt you need to specify befor setup, and in setup that another servo exists, like you have my servo, or myservo 2, so you can refer to it in loop , though ive no idea how this would be configured in what order it written in, thats what i find misleading in the tutorial,

Adamkdrums:
erm, kida, wouldnt you need to specify befor setup, and in setup that another servo exists, like you have my servo, or myservo 2, so you can refer to it in loop ,

Yes.

The best way for you to learn and for us to help is if you have a go at writing the code. Then if it does not work, post your latest version and we may be able to offer suggestions.

...R

this is how i think to do this atm, but obviously its not right, ?

*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

Servo myservo1;

int potpin = 1;
int val;

void setup()
{
  myservo.attach(9);
  myservo1.attach(10);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);     // waits for the servo to get there 
  
  val = analogRead(potpin1);
  val = map(val,1,1023, 0,180);
  myservo1.write(val);
  delay(15);
}

Arduino: 1.7.6 (Mac OS X), Board: "Arduino Uno"

Knob.ino:18:5: error: redefinition of 'int potpin'
Knob.ino:14:5: error: 'int potpin' previously defined here
Knob.ino:19:5: error: redefinition of 'int val'
Knob.ino:15:5: error: 'int val' previously declared here
Knob.ino: In function 'void loop()':
Knob.ino:34:20: error: 'potpin1' was not declared in this scope
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Read the first error message and then ...

Look closely at Line 29 and then at Line 13.

...R

the val of potpin 1? i dont quite follow

*/

#include <Servo.h>

Servo myservo0;  // create servo object to control a servo
Servo myservo1;

int potpin0 = 0;
int potpin1 = 1;// analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

this works,   if you wanted to do pan and tilt with a camera say,,   


void setup()
{
  myservo0.attach(9);
  myservo1.attach(10);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{ 
  val = analogRead(potpin0);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo0.write(val);                  // sets the servo position according to the scaled value 
  delay(15);     // waits for the servo to get there 
  
  val = analogRead(potpin1);
  val = map(val,0,1023, 0,180);
  myservo1.write(val);
  delay(15);
}

I don't know what Reply #8 is intended to convey?

Perhaps it means that you have got it working ?

...R

Adamkdrums:
the val of potpin 1? i dont quite follow

you have 2 potpins but only one val variable to store both values

yes the code in #8 works fine. it was mainly the order i put things in, like myservo and myservo1 together ,, then the values etc....