Go Down

Topic: Ping+Blink+Sweep=headache (Read 1 time) previous topic - next topic

AlphaBeta

This is an example implementation using the abovementioned libraries:
[UNTESTED CODE] but it compiles.
Quote

//include libraries:
#include <Servo.h>
//TimedAction.zip
#include <TimedAction.h>
//LED.zip
#include <LED.h>

//config timing
const byte BLINK_FREQUENCY = 1000;          //the led will blink every second
const byte NUMBER_OF_SCANS_PER_SECOND = 2;  //change this to suit your needs

const byte SCAN_FREQUENCY = 1000/NUMBER_OF_SCANS_PER_SECOND;

//pin number definitions
const byte LED_PIN = 13;
const byte SERVO_PIN = 9;
const byte PING_PIN = 7;

//TimedAction / 'thread' objects
TimedAction blinkAction = TimedAction(BLINK_FREQUENCY,blinkLED);
TimedAction scanAction = TimedAction(SCAN_FREQUENCY,scan);

//LED object
LED led = LED(LED_PIN);

//Servo object
Servo myservo;
int servoPosition = 120;       //start position
boolean scanIncrement = true;  //increase position?
byte servoIncrementValue = 10;
byte servoDecrementValue = 10;

void setup(){
 Serial.begin(9600);
 myservo.attach(SERVO_PIN);
}

void loop(){
 blinkAction.check();
 scanAction.check();
}

//when it's time, toggle led
void blinkLED(){ led.toggle(); }

//when it's time, scan area
void scan(){
 scanIncrement ? servoPosition+=servoIncrementValue : servoPosition-=servoDecrementValue; //increment or decrement current position
 if (servoPosition>160){
   scanIncrement = false;
   servoPosition = 160;
 } else if (servoPosition < 120){
   scanIncrement = true;
   servoPosition = 120;
 }
 myservo.write(servoPosition);
 long pingInches = ping();
 
 //DEBUG
 Serial.print(pingInches);
 Serial.print("in, ");
 Serial.print(servoPosition);
 Serial.println("deg");
 //END DEBUG
}

//return inches
long ping(){
 long duration = 0;
 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
 pinMode(PING_PIN, OUTPUT);
 digitalWrite(PING_PIN, LOW);
 delayMicroseconds(2);
 digitalWrite(PING_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(PING_PIN, LOW);
 // The same pin is used to read the signal from the PING))): a HIGH
 // pulse whose duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(PING_PIN, INPUT);
 duration = pulseIn(PING_PIN, HIGH);

 return (duration / 74 / 2);
}


This will, or at least should, periodically blink the led and scan the area using the servo and the ping sensor.

Byron Morgan

Wow. Well, I was getting somewhere with just the LED.h but i was a LONG way from that bit. Works beautifully.  And the easy adjustments are great. It amazes me how simple some of y'all make this stuff.

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

AlphaBeta

Quote
Wow. Well, I was getting somewhere with just the LED.h but i was a LONG way from that bit. Works beautifully.  And the easy adjustments are great. It amazes me how simple some of y'all make this stuff.


I almost feel bad for stealing all the fun coding it, from you.

The LED and TimedAction libraries hides some Arduino API calls, the program could've been written without the use of those libraries.
But, they do simplify things, and makes for a 'prettier' code, if you ask me.


I am glad to hear that you think it is/was/looked simple! That's what the arduino is all about :)

Glad to be of assistance, do not hesitate to ask about anything. I'll gladly try to explain.

Byron Morgan

Don't feel bad....i'd gotten past the fun point for the night, was well into the growl stage, and rapidly approching the alt-f4 go play WoW stage.

I will dive in to make sure i understand what is going on before i get much deeper. This thing is heading toward a mobile creature sometime in the future.  

Thank you again for the help. I'll surely need it again, and i'll try not to make too much of a nuisance of myself.  ;D
Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Go Up