Here is my code:
Code: Select all
#include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
int sensorReading = 0;
//int reading;
//int reading2;
int pos;
//const unsigned long eventInterval = 4000;
//unsigned long previousTime = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(led, OUTPUT);
//digitalWrite(led, HIGH);
Serial.begin(9600);
Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
// lcd.begin (16 ,2);
lcd.begin (20 ,4);
lcd.clear();
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9); //servo at digital pin 9
myservo.write(0); //initial point for servo
//myservo2.attach(11);
//myservo2.write(0);
}
void loop()
{
do
{
// unsigned long currentTime = millis();
myservo.detach();
//myservo2.detach();
//-+delay(50);
//reading=analogRead(A0);
//reading = analogRead(A0); //attached to analog 0
//reading2=analogRead(A1);
//lcd.setCursor(0, 1);
sensorReading = analogRead(knockSensor);
//if(sensorReading < threshold){ //was 900
//reading=0;
//}
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
if (sensorReading > threshold) {
//String packet = String("NP,") + millis() + String(',') + sensorReading;
//Serial.println(packet);
//digitalWrite(led, LOW);
Serial.println("Knock!");
health=health-sensorReading;
//health=health-reading-reading2;
Serial.print(sensorReading);
lcd.print(sensorReading);
//Serial.print("Sensor Value=");
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1);
}
//delay(100);
// if(reading2 < 800){
//reading2=0;
//}
/*if(reading > 0){ //was 900
Serial.println(sensorReading);
//delay(100);
lcd.println(sensorReading);
//if (currentTime - previousTime >= eventInterval) {
// reading>=280;
}
//if(reading2 > 800){
// Serial.println(reading2);
// lcd.println(reading2);
*/
//}
/*
Serial.print("Sensor Value=");
health=health-sensorReading;
//health=health-reading-reading2;
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
*/
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(1);
// myservo2.write(1);
// sets the servo position according to the scaled value
delay(1000);
// waits for it to get to the position
myservo.detach();
// myservo2.detach();
delay(6000);//how long it pauses
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(179);
//myservo2.write(179);
// sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
//myservo2.detach();
delay(1000);
health=500;
}
I also tried to replace my previous code to make the servo sweep code with some of the code from the Sweep Example Sketch. But this just made my servo motor not sweep at all. Here is the code:
Code: Select all
#include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
int sensorReading = 0;
//int reading;
//int reading2;
int pos;
//const unsigned long eventInterval = 4000;
//unsigned long previousTime = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(led, OUTPUT);
//digitalWrite(led, HIGH);
Serial.begin(9600);
Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
// lcd.begin (16 ,2);
lcd.begin (20 ,4);
lcd.clear();
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9); //servo at digital pin 9
myservo.write(0); //initial point for servo
//myservo2.attach(11);
//myservo2.write(0);
}
void loop()
{
do
{
// unsigned long currentTime = millis();
myservo.detach();
//myservo2.detach();
//-+delay(50);
//reading=analogRead(A0);
//reading = analogRead(A0); //attached to analog 0
//reading2=analogRead(A1);
//lcd.setCursor(0, 1);
sensorReading = analogRead(knockSensor);
//if(sensorReading < threshold){ //was 900
//reading=0;
//}
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
if (sensorReading > threshold) {
//String packet = String("NP,") + millis() + String(',') + sensorReading;
//Serial.println(packet);
//digitalWrite(led, LOW);
Serial.println("Knock!");
health=health-sensorReading;
//health=health-reading-reading2;
Serial.print(sensorReading);
lcd.print(sensorReading);
//Serial.print("Sensor Value=");
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1);
}
//delay(100);
// if(reading2 < 800){
//reading2=0;
//}
/*if(reading > 0){ //was 900
Serial.println(sensorReading);
//delay(100);
lcd.println(sensorReading);
//if (currentTime - previousTime >= eventInterval) {
// reading>=280;
}
//if(reading2 > 800){
// Serial.println(reading2);
// lcd.println(reading2);
*/
//}
/*
Serial.print("Sensor Value=");
health=health-sensorReading;
//health=health-reading-reading2;
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
*/
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
//Begiinning of Sweep Example Sketch
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(15); // waits 15 ms for the servo to reach the position
//Ending of Sweep Example Sketch
health=500;
}
}
Can you tell me how to make my servo motor move evenly in each direction when it sweeps? If so that would be great.