ATTiny85 Softrcpulseout and tinynunchuk

Hi, I want to use nintendo wii nunchuk to control the servo motors. The code I have is from an example by datacute

I edited this code to include the softrcpulseout.h to control the servo motors. I used the example code for this servo motor and it works perfectly on both pin1 and pin3. The servo sweeps just like intended:
#include <SoftRcPulseOut.h>
#define SERVO_PIN1 1 //P1
#define SERVO_PIN2 3 //P3
#define REFRESH_PERIOD_MS 20

SoftRcPulseOut myservo1; // create servo object to control a servo
int i = 0;

void setup() {
myservo1.attach(SERVO_PIN1);
}

void loop() {
for (i = 0; i <= 180; i++) {
myservo1.write(i); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(1);
}
for (i = 180; i >= 0; i--) {
myservo1.write(i); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(1);
}
}

The problem occurs when I try to use the tinynunchk library in combination with softrcpulseout. I want to control the servo position by using nunchuk joystick. When I include the tinynunchuk.h, the servo doesn't sweep anymore. I just edited the example code by datacute. Please help.

// To use the Wire library:
//#include <Wire.h>

// To use the Adafruit's TinyWireM library:
//#include <TinyWireM.h>

// To use the TinyI2C library from GitHub - technoblogy/tiny-i2c: Minimal I2C master routines for all AVR microcontrollers.
//#include <TinyI2CMaster.h>

#include <SoftRcPulseOut.h>
#include <TinyNunchuk.h>

#define SERVO_PIN1 1 //P1
#define SERVO_PIN2 3 //P3
#define REFRESH_PERIOD_MS 20

SoftRcPulseOut myservo1; // create servo object to control a servo

int pos = 0;
int i = 0;

void setup() {
nunchuk.begin();
myservo1.attach(SERVO_PIN1);
//myservo.attach(SERVO_PIN1);
//TinyWireM.begin(); // initialize I2C lib
}

void loop() {
nunchuk.update();
// pos = nunchuk.x;
// pos = map(pos, 26, 228, 0, 180);
for (i = 0; i <= 180; i++) {
myservo1.write(i); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(1);
}
for (i = 180; i >= 0; i--) {
myservo1.write(i); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(1);
}
}

I did it! I used arduinonunchuk.h and wire.h with digispark attiny85 and it works!

// To use the Wire library:
#include <Wire.h>

// To use the Adafruit's TinyWireM library:
//#include <TinyWireM.h>

// To use the TinyI2C library from GitHub - technoblogy/tiny-i2c: Minimal I2C master routines for all AVR microcontrollers.
//#include <TinyI2CMaster.h>

#include <SoftRcPulseOut.h>
//#include <TinyNunchuk.h>
#include <ArduinoNunchuk.h>

#define SERVO_PIN1 1 //P1
#define SERVO_PIN2 3 //P3
#define REFRESH_PERIOD_MS 20

SoftRcPulseOut myservo1; // create servo object to control a servo
ArduinoNunchuk nunchuk = ArduinoNunchuk();

int pos = 0;
int i = 0;

void setup() {
nunchuk.init();
//nunchuk.begin();
myservo1.attach(SERVO_PIN1);
//myservo.attach(SERVO_PIN1);
// TinyWireM.begin(); // initialize I2C lib
}

void loop() {
//nunchuk.update();
// pos = nunchuk.x;
// pos = map(pos, 26, 228, 0, 180);
nunchuk.update();
pos = nunchuk.analogX;
// for (i = 0; i <= 180; i++) {
// myservo1.write(i); // tell servo to go to position in variable 'pos'
// delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
// SoftRcPulseOut::refresh(1);
// }
// for (i = 180; i >= 0; i--) {
// myservo1.write(i); // tell servo to go to position in variable 'pos'
// delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
// SoftRcPulseOut::refresh(1);
// }
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(1);
}

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.