Encoder.h - trying to reset encoders to 0 from keyboard

Status
Not open for further replies.

laptophead

Well-known member
I used your TwoKnobs example and it works great. I will try to control 6 DC motors with 6 Quad encoders through 6 PIDs.
Do you think it will overwhelm the Teensy? Will they move without jerk in the same time?

I got stuck when I tried to Zero the encoders linked to a specific number (00) sent from the keyboard on my Mac. It does not reset. Also I would love to be able to send more commands from the keyboard such as "run1 100" and be able to run motor 1 , 100 steps. Am I going the right way about it?

Here is the code, why cant I find the upload anymore?!


Thanks a lot,

Mitch


/* Encoder Library - TwoKnobs Example
http://www.pjrc.com/teensy/td_libs_Encoder.html

This example code is in the public domain.
*/

#include <Encoder.h>

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

Encoder knobLeft(11, 12);
Encoder knobRight(24, 25); // small motor

// avoid using pins with LEDs attached

void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
pinMode( 13, OUTPUT);
digitalWrite (13, 1); // turn on LED to see that teensy is powered
}

long positionLeft = -999;
long positionRight = -999;

void loop() {


long newLeft, newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}

recvWithEndMarker();
showNewData();

if (receivedChars == 00) {

Serial.println("Reset both knobs to zero");
knobLeft.write(0);
knobRight.write(0);
}


} // End Lhoop




void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;

// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}

void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
 
Status
Not open for further replies.
Back
Top