hey there! im strugeling to send an encoder value from an arduino mini pro to an arduino nano. im ussing a cheap RF transmitter and receiver from sparkfun.
the trasmitter side seams to work when i print the encoder value i get positv and negativ values.
but on the receiverside i only get positiv values from 0-255
here is the trasmitter code:
#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).
*/
receiverside:
#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];
any idea how i cant manage on the receiver side to receive values like -200 +500 ?
chears and thx
janosch