Just a short question.

I´ve done two sketches, one for a sensor and another one for a servo. But i don´t know if a can mix them in one sketch so i can use just one Arduino. Thanks a lot.

This is the sensor´s code: (it´s an easy one)

const int SENSOR = 0;
int val = 0; 
void setup () {
  Serial.begin(9600);
}
void loop () {
  val = analogRead (SENSOR);
  Serial.println (val);
  delay (100);
}

And this servo´s:

#include <Servo.h>
Servo servoMain; // Define nuestro servo

void setup()
{
   servoMain.attach(10); // servo en pin 10PWM

void loop()
{
   servoMain.write(45);  // Gira 45 grados
   delay(1000);          // espera un segundo
   servoMain.write(0);   // gira 0 grados
   delay(1000);          // espera un segundo
   servoMain.write(90);  // gira al centro, 90 grados
   delay(1000);          // espera un segundo
   servoMain.write(135); // gira 135 grados
   delay(1000);          // espera un segundo
   servoMain.write(180); // gira 180 grados
   delay(1000);          // espera un segundo
   delay(1000);          // espera un segundo  
}

I´ve tried to mix them apparently in a correct way but when i open Processing the sensor doesn´t run. Thank you very much again.

Moderator edit: Replaced italics tags with CODE TAGS

Yes you can.
Or, maybe not.

How can we tell?

(That's a hint, by the way. You did read the sticky thread at the top of the programming section, didn't you?)

Yes, you can merge them.
This usually involves getting rid of all the calls to "delay()".

I´ve tried to mix them apparently in a correct way

But you didn't show us it.

That´s my aim of merging codes.

const int SENSOR = 0;
int val = 0; 
#include <Servo.h>
Servo servoMain; // Define nuestro servo

void setup () {
  Serial.begin(9600);
  servoMain.attach(10); // servo en pin10
  }

void loop () {
  val = analogRead (SENSOR);
  Serial.println (val);
  delay (100);
  servoMain.write(45);  // Gira 45 grados
   delay(1000);          // espera un segundo
   servoMain.write(0);   // gira 0 grados
   delay(1000);          // espera un segundo
   servoMain.write(90);  // gira al centro, 90 grados
   delay(1000);          // espera un segundo
   servoMain.write(135); // gira 135 grados
   delay(1000);          // espera un segundo
   servoMain.write(180); // gira 180 grados
   delay(1000);          // espera un segundo
   delay(1000);          // espera un segundo  
}

When I compile is ok but the sensor doesn´t run.

Moderator edit: CODE TAGS again.

Sensors don't typically run.
I assume you mean "the sensor doesn't produce results every 1/10th of a second as it did before, but only every six seconds"
Yes, that's what I'd expect, which is why you need to get rid of the delays as they are written now.

I use the word "run" because is the one i know, sorry for my english. :slight_smile:

I´ll try, thank very much.

You could write your own version of "delay" which works in 100 millisecond chunks, reading and printing the sensor value every 100 milliseconds (quick-and-fairly-dirty method), or you could take a look at the blink without delay example which came with the IDE.

Sorry, i´m trying but i don´t know the way of changing "delay" for another one.

What have you tried?

The blink without delay example, i mean: using millis.

The blink without delay example, i mean: using millis.

The correct answer is "I looked at the blink without delay example, and came up with this code:".

Your answer is only half right.