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!