Go Down

Topic: Just a short question. (Read 430 times) previous topic - next topic

Ainajhara

Jan 10, 2013, 12:37 pm Last Edit: Jan 10, 2013, 12:52 pm by AWOL Reason: 1
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)

Code: [Select]
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:

Code: [Select]
#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

AWOL

#1
Jan 10, 2013, 12:42 pm Last Edit: Jan 10, 2013, 12:44 pm by AWOL Reason: 1
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?)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

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

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

But you didn't show us it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ainajhara

#3
Jan 10, 2013, 01:23 pm Last Edit: Jan 10, 2013, 01:27 pm by AWOL Reason: 1
That´s my aim of merging codes.

Code: [Select]
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.

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ainajhara

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

I´ll try, thank very much.

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ainajhara

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

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ainajhara

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

PaulS

Quote
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.

Go Up