Nope- didn’t work. Maybe I inserted it wrong. Here’s the code I’m writing:
#include <Wire.h>
int controlPin1 = 11; // Control pin for arm servo using c button
int transistorPin1 = 12; // Control pin for LED using z button
int transistorPin2 = 10; // Control pin for sound using z button
int servoPin1 = 7; // Control pin for servo motor 1 using accelerometer x axis
int servoPin2 = 6; // Control pin for servo motor 2 using accelerometer y axis
int servoPin3 = 5; // Control pin for servo motor 3 using joystick x axis
int servoPin4 = 4; // Control pin for servo motor 4 using joystick y axis
int pulseWidth1 = 0; // Amount to pulse the servo 1
int pulseWidth2 = 0; // Amount to pulse the servo 2
int pulseWidth3 = 0; // Amount to pulse the servo 3
int pulseWidth4 = 0; // Amount to pulse the servo 4
int refreshTime = 20; // the time in millisecs needed in between pulses
long lastPulse1;
long lastPulse2;
long lastPulse3;
long lastPulse4;
int minPulse = 700; // minimum pulse width
int loop_cnt=0;
bool button_down = false;
unsigned long start;
void setup()
{
Serial.begin(19200);
pinMode(controlPin1, OUTPUT); // Set control pin 1 as output
pinMode(transistorPin1, OUTPUT); // Set transistor pin 1 as output
pinMode(transistorPin2, OUTPUT); // Set transistor pin 2 as output
pinMode(servoPin1, OUTPUT); // Set servo pin 1 as an output pin
pinMode(servoPin2, OUTPUT); // Set servo pin 2 as an output pin
pinMode(servoPin3, OUTPUT); // Set servo pin 3 as an output pin
pinMode(servoPin4, OUTPUT); // Set servo pin 4 as an output pin
pulseWidth1 = minPulse; // Set the motor position to the minimum
pulseWidth2 = minPulse; // Set the motor position to the minimum
pulseWidth3 = minPulse; // Set the motor position to the minimum
pulseWidth4 = minPulse; // Set the motor position to the minimum
nunchuck_init(); // send the initilization handshake
Serial.print("NunchuckServo ready\n");
}
void loop()
{
checkNunchuck1();
updateServo1(); // update servo 1 position
checkNunchuck2();
updateServo2(); // update servo 2 position
checkNunchuck3();
updateServo3(); // update servo 3 position
checkNunchuck4();
updateServo4(); // update servo 4 position
if( nunchuck_cbutton() ) { // turn on control pin 1 if c button is pressed
digitalWrite(controlPin1, HIGH);
}
else {
digitalWrite(controlPin1, LOW);
}
if (nunchuck_zbutton())
{
if (!button_down) // If button was just pressed do this
{
button_down = true;
start = millis();
digitalWrite(transistorPin2, HIGH);
}
else if (millis() - start > 2000) // if timer has elapsed do this
{
digitalWrite(transistorPin1, HIGH);
}
}
else // if button is up do this
{
button_down = false;
digitalWrite(transistorPin2, LOW);
digitalWrite(transistorPin1, LOW);
}
delay(1); // this is here to give a known time per loop
}
void checkNunchuck1()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
nunchuck_get_data();
nunchuck_print_data();
float tilt = nunchuck_accelx(); // x-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth1 = (tilt * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo1()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse1 >= refreshTime) {
digitalWrite(servoPin1, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth1); // Length of the pulse sets the motor position
digitalWrite(servoPin1, LOW); // Turn the motor off
lastPulse1 = millis(); // save the time of the last pulse
}
}
void checkNunchuck2()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
nunchuck_get_data();
nunchuck_print_data();
float tilt = nunchuck_accely(); // y-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth2 = (tilt * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo2()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse2 >= refreshTime) {
digitalWrite(servoPin2, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth2); // Length of the pulse sets the motor position
digitalWrite(servoPin2, LOW); // Turn the motor off
lastPulse2 = millis(); // save the time of the last pulse
}
}
void checkNunchuck3()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
nunchuck_get_data();
nunchuck_print_data();
float pitch = nunchuck_joyx(); // x-axis, in this case ranges from ~70 - ~185
pitch = (pitch - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth3 = (pitch * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo3()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse3 >= refreshTime) {
digitalWrite(servoPin3, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth3); // Length of the pulse sets the motor position
digitalWrite(servoPin3, LOW); // Turn the motor off
lastPulse3 = millis(); // save the time of the last pulse
}
}
void checkNunchuck4()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
nunchuck_get_data();
nunchuck_print_data();
float yaw = nunchuck_joyy(); // y-axis, in this case ranges from ~70 - ~185
yaw = (yaw - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth4 = (yaw * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo4()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse4 >= refreshTime) {
digitalWrite(servoPin4, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth4); // Length of the pulse sets the motor position
digitalWrite(servoPin4, LOW); // Turn the motor off
lastPulse4 = millis(); // save the time of the last pulse
}
}