Moin zusammen
Ich versuche jetzt schon seid einer weile meinen Encoderwert von einem Arduino Mini Pro zu einem Arduio Nano zu schicken und werde bald verückt
Ich benutze einen rf receiver und transiver von watterot
und die virtualwire.h library
bei der Transmitterseite scheint alles zufunktionieren:
#include <VirtualWire.h>
#define encoder0PinA 2
#define encoder0PinB 14
signed int numbers[3];
volatile signed int encoder0Pos = 125;
void setup()
{
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE);
vw_set_ptt_inverted(true);
vw_setup(2000);
Serial.begin (9600);
Serial.println("start");
}
void loop()
{
numbers[0] = encoder0Pos;
vw_send( (uint8_t *)numbers, sizeof(numbers));
vw_wait_tx(); // Wait for message to finish
delay(50);
Serial.println(numbers[0], DEC);
}
void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning
* forward. If they're different, it's going backward.
*
* For more information on speeding up this process, see
* [Reference/PortManipulation], specifically the PIND register.
*/
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}
//Serial.print("encoderpos:");
// Serial.println (encoder0Pos, DEC);
}
/* See this expanded function to get a better understanding of the
* meanings of the four possible (pinA, pinB) value pairs:
*/
void doEncoder_Expanded(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
//Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
// you don't want serial slowing down your program if not needed
}
/* to read the other two transitions - just use another attachInterrupt()
in the setup and duplicate the doEncoder function into say,
doEncoderA and doEncoderB.
You also need to move the other encoder wire over to pin 3 (interrupt 1).
*/
Serial.println(numbers[0], DEC); liefert mir so wie ich das wollte positive und negative werte.
bei der receiverseite habe ich da weniger glück ich bekomme es dort nicht hin negative werte zu empfangen, bekomme immer nur von 0-255
hier der code:
#include <VirtualWire.h>
uint8_t buf[3];
uint8_t buflen;
signed int encoder= 0;
signed long istpos = 125;
signed long abweichung = 0;
signed long sollpos = 0;
void setup()
{
buflen = 3;
Serial.begin(9600);
Serial.println("start");
vw_setup(2000);
vw_rx_start();
}
void loop()
{
vw_get_message(buf, &buflen);
Serial.println(buf[0],DEC);
encoder = buf[0];
ist nur ein auschnitt aus dem code...
ich will auf der receiver seite damit einen stepper drehen und brauche hierfür sowohl positive als auch negative werte das ist ja das schöne an einem encoder sonst hätte ich gleich einen poti nehmen können
wie gesagt nochmal zusammengefast:
Die Transmitter seite also das numbers[0] enthält den richtigen encoder wert also positive und negative werte.
und bei der receiverseite bekomme ich es nicht hin das er diesen wert richtig emfängt
hat jemand eine idee?
lg und danke janosch