serial input mapped to servo?
i have both these sketches , running i'd map sonar servo.
is possible? unfortunately i'm total noob. thank help.
sonar test code: ( i'm reading output on serial )
/*
hc-sr04 ping distance sensor]
vcc arduino 5v gnd arduino gnd
echo arduino pin 13 trig arduino pin 12
red pos arduino pin 11
green pos arduino pin 10
560 ohm resistor both led neg , grd power rail
more info at: http://goo.gl/kj8gl
original code improvements ping sketch sourced trollmaker.com
some code , wiring inspired http://en.wikiversity.org/wiki/user:dstaub/robotcar
*/
#define trigpin 12
#define echopin 13
#define led 11
#define led2 10
void setup() {
serial.begin (9600);
pinmode(trigpin, output);
pinmode(echopin, input);
pinmode(led, output);
pinmode(led2, output);
}
void loop() {
long duration, distance;
digitalwrite(trigpin, low); // added line
delaymicroseconds(2); // added line
digitalwrite(trigpin, high);
// delaymicroseconds(1000); - removed line
delaymicroseconds(10); // added line
digitalwrite(trigpin, low);
duration = pulsein(echopin, high);
distance = (duration/2) / 29.1;
if (distance < 4) { // led on/off happens
digitalwrite(led,high); // when red condition met, green led should turn off
digitalwrite(led2,low);
}
else {
digitalwrite(led,low);
digitalwrite(led2,high);
}
if (distance >= 200 || distance <= 0){
serial.println("out of range");
}
else {
serial.print(distance);
serial.println(" cm");
}
delay(500);
}
----------------------------------------------------
servo sweep code :
/* sweep
by barragan <http://barraganstudio.com>
this example code in public domain.
modified 8 nov 2013
by scott fitzgerald
http://www.arduino.cc/en/tutorial/sweep
*/
#include <servo.h>
servo myservo; // create servo object control servo
// twelve servo objects can created on boards
int pos = 0; // variable store servo position
void setup() {
myservo.attach(9); // attaches servo on pin 9 servo object
}
void loop() {
(pos = 0; pos <= 180; pos += 1) { // goes 0 degrees 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach position
}
(pos = 180; pos >= 0; pos -= 1) { // goes 180 degrees 0 degrees
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach position
}
}
is possible? unfortunately i'm total noob. thank help.
sonar test code: ( i'm reading output on serial )
/*
hc-sr04 ping distance sensor]
vcc arduino 5v gnd arduino gnd
echo arduino pin 13 trig arduino pin 12
red pos arduino pin 11
green pos arduino pin 10
560 ohm resistor both led neg , grd power rail
more info at: http://goo.gl/kj8gl
original code improvements ping sketch sourced trollmaker.com
some code , wiring inspired http://en.wikiversity.org/wiki/user:dstaub/robotcar
*/
#define trigpin 12
#define echopin 13
#define led 11
#define led2 10
void setup() {
serial.begin (9600);
pinmode(trigpin, output);
pinmode(echopin, input);
pinmode(led, output);
pinmode(led2, output);
}
void loop() {
long duration, distance;
digitalwrite(trigpin, low); // added line
delaymicroseconds(2); // added line
digitalwrite(trigpin, high);
// delaymicroseconds(1000); - removed line
delaymicroseconds(10); // added line
digitalwrite(trigpin, low);
duration = pulsein(echopin, high);
distance = (duration/2) / 29.1;
if (distance < 4) { // led on/off happens
digitalwrite(led,high); // when red condition met, green led should turn off
digitalwrite(led2,low);
}
else {
digitalwrite(led,low);
digitalwrite(led2,high);
}
if (distance >= 200 || distance <= 0){
serial.println("out of range");
}
else {
serial.print(distance);
serial.println(" cm");
}
delay(500);
}
----------------------------------------------------
servo sweep code :
/* sweep
by barragan <http://barraganstudio.com>
this example code in public domain.
modified 8 nov 2013
by scott fitzgerald
http://www.arduino.cc/en/tutorial/sweep
*/
#include <servo.h>
servo myservo; // create servo object control servo
// twelve servo objects can created on boards
int pos = 0; // variable store servo position
void setup() {
myservo.attach(9); // attaches servo on pin 9 servo object
}
void loop() {
(pos = 0; pos <= 180; pos += 1) { // goes 0 degrees 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach position
}
(pos = 180; pos >= 0; pos -= 1) { // goes 180 degrees 0 degrees
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach position
}
}
please use [ code ] tags next time, "how use forum" shows you.
well, think map() function might useful you. read how works. try map distance (or duration) pos (position).
well, think map() function might useful you. read how works. try map distance (or duration) pos (position).
Arduino Forum > Using Arduino > Programming Questions > serial input mapped to servo?
arduino
Comments
Post a Comment