serial input and output at the same time for tacho
i want control continuous rotation servo using serial input (ie < or >) , read out showing me rpm using optical tachometer. have both codes working independently, want fuse them together. successful @ putting analog servo control tachometer, having terrible time putting serial controls place. here codes:
thanks much. i've been working on while , need work.
quote
/*
* optical tachometer
*
* uses ir led , ir phototransistor implement optical tachometer.
* ir led connected pin 13 , ran continually. status led connected
* pin 12. pin 2 (interrupt 0) connected across ir detector.
*
*
*/
int ledpin = 13; // ir led connected digital pin 13
int statuspin = 12; // led connected digital pin 12
volatile byte rpmcount;
volatile int status;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
//each rotation, interrupt function run twice, take consideration for
//calculating rpm
//update count
rpmcount++;
//toggle status led
if (status == low) {
status = high;
} else {
status = low;
}
digitalwrite(statuspin, status);
}
void setup()
{
serial.begin(9600);
//interrupt 0 digital pin 2, ir detector connected
//triggers on falling (change high low)
attachinterrupt(0, rpm_fun, falling);
//turn on ir led
pinmode(ledpin, output);
digitalwrite(ledpin, high);
//use statuspin flash along interrupts
pinmode(statuspin, output);
rpmcount = 0;
rpm = 0;
timeold = 0;
status = low;
}
void loop()
{
//update rpm every second
delay(1000);
//don't process interrupts during calculations
detachinterrupt(0);
//note 60*1000/(millis() - timeold)*rpmcount if interrupt
//happened once per revolution instead of twice. other multiples used
//for multi-bladed propellers or fans
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
//write out serial port
serial.println(rpm,dec);
//restart interrupt processing
attachinterrupt(0, rpm_fun, falling);
}
quote
/*
* serial port continuous rotation servo - movement , speed control
* --------------
*
* alteration of control interface use < , > keys
* slew servo horn left , right. works best with
* linux/mac terminal "screen" program.
*
* adapted continuous rotation servo (parallax)
* orfeus
* v.200910211932
*
* serial port terminal on windows used putty ssh client
*
*
*
*
*
* created 10 december 2007
* copyleft 2007 brian d. wendt
*
*
* adapted code tom igoe
*
*
*/
/** adjust these values servo , setup, if necessary **/
int servopin = 9; // control pin servo motor
int minpulse = 1170; // maximum servo speed clockwise
int maxpulse = 1770; // maximum servo speed anticlockwise
int turnrate = 50; // servo turn rate increment (larger value, faster rate)
int refreshtime = 20; // time (ms) between pulses (50hz)
/** arduino calculate these values **/
int centerservo; // center servo position
int pulsewidth; // servo pulse width
int moveservo; // raw user input
long lastpulse = 0; // recorded time (ms) of last pulse
void setup() {
pinmode(servopin, output); // set servo pin output pin
centerservo = maxpulse - ((maxpulse - minpulse)/2);
pulsewidth = centerservo; // give servo stop command
serial.begin(9600);
serial.println("arduino serial continuous rotation servo control");
serial.println(" by orfeus grobot.gr");
serial.println(" press < or > move, spacebar center");
serial.println();
}
void loop() {
// wait serial input
if (serial.available() > 0) {
// read incoming byte:
moveservo = serial.read();
// ascii '<' 44, ascii '>' 46 (comma , period, really)
if (moveservo == 44) { pulsewidth = pulsewidth + turnrate; }
if (moveservo == 46) { pulsewidth = pulsewidth - turnrate; }
if (moveservo == 32) { pulsewidth = centerservo; }
// stop servo pulse @ min , max
if (pulsewidth > maxpulse) { pulsewidth = maxpulse; }
if (pulsewidth < minpulse) { pulsewidth = minpulse; }
// show me keys pressed
//serial.print("key pressed: ");
//serial.println(moveservo);
//print pulsewidth serial monitor (comment undebug)
serial.print("pulse width: ");
serial.print(pulsewidth);
serial.println("us");
}
// pulse servo every 20 ms (refreshtime) current pulsewidth
// hold servo's rotation , speed till told else.
if (millis() - lastpulse >= refreshtime) {
digitalwrite(servopin, high); // start pulse
delaymicroseconds(pulsewidth); // pulse width
digitalwrite(servopin, low); // stop pulse
lastpulse = millis(); // save time of last pulse
}
}
/* orfeus 200910212001 */
thanks much. i've been working on while , need work.
which correct? 44 < or comma?
there nothing wrong with:
if(moveservo == '>') // something
and there no reason consult table find proper value, when want change value.
why sending . , , increase or decrease values? least obvious characters send. < , > or u , d make sense. . , , not.
i guess it's not important real-time, huh?
code: [select]
// ascii '<' 44, ascii '>' 46 (comma , period, really)
if (moveservo == 44) { pulsewidth = pulsewidth + turnrate; }
if (moveservo == 46) { pulsewidth = pulsewidth - turnrate; }
if (moveservo == 32) { pulsewidth = centerservo; }there nothing wrong with:
if(moveservo == '>') // something
and there no reason consult table find proper value, when want change value.
why sending . , , increase or decrease values? least obvious characters send. < , > or u , d make sense. . , , not.
code: [select]
void loop()
{
//update rpm every second
[glow]delay(1000);[/glow]i guess it's not important real-time, huh?
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > serial input and output at the same time for tacho
arduino
Comments
Post a Comment