void loop(){
if (digitalRead(tasterPin)==LOW){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
}
and here is my servo code:
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
turnRight();
delay(2000);
turnLeft();
delay(2000);
stopRobot();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
Just merge the global declarations, the contents of the setup() function and the contents of the loop() function:
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
const int ledPin = 7;
const int tasterPin = 4;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(tasterPin, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() {
if (digitalRead(tasterPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
turnRight();
delay(2000);
turnLeft();
delay(2000);
stopRobot();
delay(2000);
}
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
Oh, sorry Don. Put the code in code tags, so it will be properly formatted in a monospaced font, and the characters that are also bbcode mark-up, like [ and / do not get misinterpreted by the forum software. It's all in a link called "How to use this forum - please read" that explains how to do that. Here's the link :
ChrisTenone:
Oh, sorry Don. Put the code in code tags, so it will be properly formatted in a monospaced font, and the characters that are also bbcode mark-up, like [ and / do not get misinterpreted by the forum software. It's all in a link called "How to use this forum - please read" that explains how to do that. Here's the link :
Hey Duane, I like 'em too! Clickable links are a marvelous invention. Unfortunately when used with 'Copy Link", sometimes (I'm not sure of the exact circumstances) an 'iurl' (rather than a url) link is generated that does not build a correct link. I now see that the 'always works' way to post a link is to go to the page desired for the link, and copy its url from the browser's navigation bar, then paste it between url tags.
I will be more careful to do that in the future. I was indeed guilty of 'tag hipocracy'.
... However, leaving out url tags makes a bit more work for the reader. Leaving out code tags can mangle code by interpreting the text of the code as bbcode. Look through un-tagged code, and you will occasionally see mysterious smilies, or half the code is suddenly in italics or bold.
@ChrisTenone, I always work with my editor in "Source" mode (see the right-most icon, and settings in your Profile) and that problem of iurls goes away.
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
const int ledPin = 7;
const int tasterPin = 4;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(tasterPin, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() {
if (digitalRead(tasterPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
turnRight();
delay(2000);
turnLeft();
delay(2000);
stopRobot();
delay(2000);
}
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}