Make a servomotor and a potentiometer work in parallel

Hello!
This is my very first project with Arduino, it is an automate created from old typewriter parts, in its eye there is an rgb led connected to a 10k potentiometer (to change the color), and two servomotors in its head, operated by an ultrasonic sensor HC-SR04.
All this is managed by an Arduino Nano ATmega328P clone.
The task of the automaton is to trigger moving parts in its head when we approach within a meter, and I would like us to be able to adjust the color of the eye.
Atually it only works separetely.
Being new to programming I mixed several codes and did a lot of research on the net to find a solution to my problem, especially on the arduino forum, without success yet.
I have two codes compiled and running:
The first, below, operates the servomotors in a loop, without using the HC-SR04 sensor. But it can adjust the rgb LED.

#include <Servo.h>

Servo myservo1;
Servo myservo2;
 
int pos = 0; 

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu);

const int ledRouge=3; 
const int ledVert=5; 
const int ledBleu=6; 

int tension, val;
  
void setup() 
{
  
  pinMode (ledVert,OUTPUT);
  pinMode (ledRouge,OUTPUT);
  pinMode (ledBleu,OUTPUT);
  
  myservo1.attach(9);
  myservo2.attach(10);

 
}

void loop() {
  for (pos = 50; pos <= 110; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    myservo2.write(pos);
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 110; pos >= 50; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo1.write(pos);
    myservo2.write(pos);
    delay(20);                       // waits 15ms for the servo to reach the position
  }
  tension = analogRead(A0);

val = map(tension,0,1023,0,2*255);

if( val < 255)
ledRVBpwm(0,val,255-val);

if( val >= 255 && val <= 2*255)
ledRVBpwm(val-255, 255-(val-255),0);

}

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu) { // reçoit valeur 0-255 par couleur

analogWrite(ledRouge, pwmRouge);
analogWrite(ledVert, pwmVert);
analogWrite(ledBleu, pwmBleu);

  
}

The second, here, triggers the two servomotors via the HC-SR04 sensor.

#include<Servo.h>

Servo myservo1; 
Servo myservo2;

const int trigPin = 2;

const int echoPin = 11;

void setup() {



Serial.begin(9600); 

myservo1.attach(9);
myservo2.attach(10); 

}

void loop() {



long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(2);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 1 && cm < 100)

{

myservo1.write(50); 
myservo2.write(50);
delay(2000);

}

else if ( cm > 100)

{

myservo1.write(110); 
myservo2.write(110);
delay(100);

}

else

{

myservo1.write(50);
myservo2.write(50);
delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;

}

I try to get a mixture of the two, because I want to be able to activate the servomotors via the ultrasonic sensor, and also that we can set the led to the color of our choice. At the moment I am there:

#include<Servo.h>

Servo myservo1; 
Servo myservo2;

const int trigPin = 2;

const int echoPin = 11;

int pos = 0; 

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu);

const int ledRouge=3; 
const int ledVert=5; 
const int ledBleu=6; 

int tension, val;


void setup() {

pinMode (ledVert,OUTPUT);
pinMode (ledRouge,OUTPUT);
pinMode (ledBleu,OUTPUT);

Serial.begin(9600); 

myservo1.attach(9);
myservo2.attach(10); 

}

void loop() {



long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(2);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 1 && cm < 100)

{

myservo1.write(50); 
myservo2.write(50);
delay(2000);

}

else if ( cm > 100)

{

myservo1.write(110); 
myservo2.write(110);
delay(100);

}

else

{

myservo1.write(50);
myservo2.write(50);
delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;

}

tension = analogRead(A0);

val = map(tension,0,1023,0,2*255);

if( val < 255)
ledRVBpwm(0,val,255-val);

if( val >= 255 && val <= 2*255)
ledRVBpwm(val-255, 255-(val-255),0);

}

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu) { // reçoit valeur 0-255 par couleur

analogWrite(ledRouge, pwmRouge);
analogWrite(ledVert, pwmVert);
analogWrite(ledBleu, pwmBleu);

}

and here is the detailed error message

sketch_mar31a:108:1: error: 'tension' does not name a type

 tension = analogRead(A0);

 ^~~~~~~

sketch_mar31a:110:1: error: 'val' does not name a type

 val = map(tension,0,1023,0,2*255);

 ^~~

sketch_mar31a:112:1: error: expected unqualified-id before 'if'

 if( val < 255)

 ^~

sketch_mar31a:115:1: error: expected unqualified-id before 'if'

 if( val >= 255 && val <= 2*255)

 ^~

sketch_mar31a:118:1: error: expected declaration before '}' token

 }
exit status 1

'tension' does not name a type

Thank you for your help and advices to all of you!

check your {} and functions (press ctrl-T in the IDE to indent) you'll see that the code is not right

tension = analogRead(A0);

val = map(tension, 0, 1023, 0, 2 * 255);

if ( val < 255)
  ledRVBpwm(0, val, 255 - val);

if ( val >= 255 && val <= 2 * 255)
  ledRVBpwm(val - 255, 255-(val - 255), 0);

}

seems to be hanging outside any function

Thank you for such a fast answer! I'm doing this.

I've added an integer for the potentiometer, but i don't get how i can link it to the value of the tension...

#include<Servo.h>

Servo myservo1;
Servo myservo2;

const int trigPin = 2;

const int echoPin = 11;

int potPin = A0; // select the input pin for the potentiometer
int tension, val; // variable to store the value coming from the potentiometer


void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu);

const int ledRouge = 3;
const int ledVert = 5;
const int ledBleu = 6;




void setup() {

  pinMode (ledVert, OUTPUT);
  pinMode (ledRouge, OUTPUT);
  pinMode (ledBleu, OUTPUT);

  Serial.begin(9600);

  myservo1.attach(9);
  myservo2.attach(10);

}

void loop() {



  long duration, cm;

  pinMode(trigPin, OUTPUT);

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(2);

  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);

  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);

  // the condition for the distance

  if ( cm > 1 && cm < 100)

  {

    myservo1.write(50);
    myservo2.write(50);
    delay(2000);

  }

  else if ( cm > 100)

  {

    myservo1.write(110);
    myservo2.write(110);
    delay(100);

  }

  else

  {

    myservo1.write(50);
    myservo2.write(50);
    delay(100);

  }

  Serial.print(cm);

  Serial.print("cm");

  Serial.println();

  delay(100);

}

long microsecondsToCentimeters(long microseconds) {

  return microseconds / 29 / 2;

}

tension = analogRead(A0);

val = map(tension, 0, 1023, 0, 2 * 255);

if ( val < 255)
  ledRVBpwm(0, val, 255 - val);

if ( val >= 255 && val <= 2 * 255)
  ledRVBpwm(val - 255, 255-(val - 255), 0);

}

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu) { // reçoit valeur 0-255 par couleur

  analogWrite(ledRouge, pwmRouge);
  analogWrite(ledVert, pwmVert);
  analogWrite(ledBleu, pwmBleu);

}
tension = analogRead(A0);

val = map(tension, 0, 1023, 0, 2 * 255);

aka

val = analogRead(A0) / 2;

Assuming "tension" == "voltage", the voltage of what?

It is supposed to be the voltage of the current going through the potentiometer...

Do you need the voltage, or just some value proportional to the pot's position?

Thank you for your answer: i just need a proportional value, not a specific voltage ...

I found this sketch

// Init the Pins used for PWM
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
 
// Init the Pins used for 10K pots
const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;
 
// Init our Vars
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}
 
void loop()
{
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
 
// Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
 
}

But it works with 3 potentiometers... and with specific voltage.

I finally founded some help, and i have a code running. It was mainly a lack of fonction definition.

#include <Servo.h>

// Declaration des variables globales
Servo myservo1;
Servo myservo2;
int pos = 0;
int tension, val;

// Declaration  des constantes globales
const int trigPin = 2;
const int echoPin = 11;
const int ledRouge = 3;
const int ledVert = 5;
const int ledBleu = 6;

// Declaration des fonctions
void commande_servos_1_2();
void commande_led();
void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu);
long microsecondsToCentimeters(long microseconds);

void setup() {
  // Brochage des composants
  pinMode (ledVert,OUTPUT);
  pinMode (ledRouge,OUTPUT);
  pinMode (ledBleu,OUTPUT);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  myservo1.attach(9);
  myservo2.attach(10);

  // Activation de la liaison serie
  Serial.begin(9600);
}

void loop() {
  // Programme principal
  commande_servos_1_2();
  commande_led();
}

void commande_led() {
  // modifier la couleur de la LED RVB selon valeur potentiometre
  tension = analogRead(A0);
  val = map(tension,0,1023,0,2*255);

  if (val < 255)
  ledRVBpwm(0,val,255-val);

  if (val >= 255 && val <= 2*255)
  ledRVBpwm(val-255, 255-(val-255),0);
}

void commande_servos_1_2() {
  // declenche les deux servomoteurs via le capteur HC-SR04.
  long duration, cm;
   
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH);
  delay(100);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print("duree : ");
  Serial.print(duration);
  Serial.print(" - distance : ");
  Serial.print(cm);
  Serial.println();

  if (cm > 1 && cm < 100) {
    myservo1.write(50);
    myservo2.write(50);
    delay(2000);
  }

  else if (cm > 100) {
    myservo1.write(110);
    myservo2.write(110);
    delay(100);
  }

  else  {
    myservo1.write(50);
    myservo2.write(50);
    delay(100);
  }

  // DEBUG SERIE : distance en cm calculee
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
}

long microsecondsToCentimeters(long microseconds) {
  // convertis une duree en distance
  return microseconds / 29 / 2;
}

void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu) {
  // Controle de la led RVB : valeurs de 0 a  255
  analogWrite(ledRouge, pwmRouge);
  analogWrite(ledVert, pwmVert);
  analogWrite(ledBleu, pwmBleu);

}

beruart:
It was mainly a lack of fonction definition.

Why didn't the Arduino build process provide one?

Technically in C++ it's required to define what you use before you use it, so yes it would make sense to either declare the signature of the functions (or the functions themselves) before setup() and loop() but the Arduino IDE will have your back and during the sketch analysis process (if it's a .ino) will create the declaration for you automagically.

As a simple proof, remove them and you'll see it compiles totally fine without

// Declaration des fonctions
void commande_servos_1_2();
void commande_led();
void ledRVBpwm(int pwmRouge, int pwmVert, int pwmBleu);
long microsecondsToCentimeters(long microseconds);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.