Pages: [1]   Go Down
Author Topic: Just a short question.  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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
« Last Edit: January 10, 2013, 06:52:19 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24427
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
« Last Edit: January 10, 2013, 06:44:11 am by AWOL » Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24427
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That´s my aim of merging codes.

Code:
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.
« Last Edit: January 10, 2013, 07:27:11 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24427
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I´ll try, thank very much.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24427
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24427
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What have you tried?
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46213
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: