Hi All
I’ve been playing with the HW servo code from PhotoPete and it’s working well. I just can’t get it to run without being connected via USB to the laptop. With just external power nothing happens at all apart from a little juddering. Just from the USB and there’s not enough power it seems. I’ve got a 1A 5v USB wart going into VIN on the Uno and with USB it’s all good.
Can anyone see what’s tripping me up?
Any help appreciated.
Code:
/************************************************
Arduino Example Code to send PPM servo signals via 4017 decade counter chip.
-- HWServo4017.pde --
Created: 9/30/10
Last Modified: 11/08/2010
Written by Petey the Programmer
************************************************/
#include <ctype.h>
#include <math.h>
#include <avr/io.h>
#define ON 1
#define OFF 0
#define OneSecond 1000
#define ledPin 13
// --------------------------------------------------------------------------------------------------------------------------------------------
void Servo_4017_init( void );
void Servo_4017_Set_Pulse_Width( int ServoNum, int pulseWidth );
// --------------------------------------------------------------------------------------------------------------------------------------------
/* Global Variable definitions */
uint16_t sWidth[8], sInc[8];
unsigned long myTimer = 0; // general purpose timer
unsigned long OneSecTimer = 0;
int LED_On = 0;
// --------------------------------------------------------------------------------------------------------------------------------------------
// Data for dancing sequence
int PWM_SEQ[4][8] = {
{
2000,2000,2000,2000,2000,2000,2000,2000 }
,
{
1000,1000,1000,1000,1000,1000,1000,1000 }
,
{
1000,1000,1000,1000,2000,2000,2000,2000 }
,
{
2000,2000,2000,2000,1000,1000,1000,1000 }
};
// --------------------------------------------------------------------------------------------------------------------------------------------
void blink_led(void)
{
int n;
for (n = 0; n < 5; n++) {
digitalWrite(ledPin, 0);
delay(20);
digitalWrite(ledPin,1);
delay(20);
}
}
// -----------------------------------------------------------------------------------------------------
void setup()
{
int n;
Serial.begin(19200); // debug output over serial line
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ON);
LED_On = 1;
delay(OneSecond);
Serial.println("---");
Serial.println("Arduino 4017 Servo Driver v1.0");
delay(OneSecond);
digitalWrite(ledPin, OFF);
for (n=0; n<8; n++) // Set all servos to their center positions.
{
sWidth[n] = 1500;
sInc[n] = n * 3; // setup each servo to move at a different speed
}
sInc[0] = 2;
Servo_4017_init(); /* Init Timer1 and setup Output Compare interrupt */
blink_led();
digitalWrite(ledPin, ON);
OneSecTimer = millis();
myTimer = millis();
}
// -----------------------------------------------------------------------------------------------------
// Every 100 ms, change the pulse width being sent to each servo.
// Interrupt routines grab the sWidth array elements as it needs them.
void precision_loop ()
{
int n;
if ((millis() - myTimer) >= 20) // 100ms is arbitrary interval for demo
{
myTimer = millis();
for (n=0; n<8; n++) // Keep servo 0 at center position - move all the rest. Demos NO jitter on Servo 0.
{
sWidth[n] += sInc[n];
Servo_4017_Set_Pulse_Width(n, sWidth[n]);
if ((sWidth[n] > 2100) || (sWidth[n] < 900)) // reverse direction at end of travel
sInc[n] = -sInc[n];
}
}
if ((millis() - OneSecTimer) >= OneSecond) // this goes off once per second
{
OneSecTimer = millis();
LED_On = !LED_On;
digitalWrite(ledPin, LED_On);
}
}
// -----------------------------------------------------------------------------------------------------
// from ArduPilot Mega Dance demo
void dance_loop()
{
for(int w=0; w<=5; w++)
{
for(int y=0; y<2; y++)
{
for(int x=0; x<=7; x++)
{
Servo_4017_Set_Pulse_Width(x, PWM_SEQ[y][x]);
delay(100);
}
}
}
/* -------- */
for(int w=0; w<=5; w++)
{
for(int y=2; y<4; y++)
{
for(int x=0; x<=7; x++)
{
Servo_4017_Set_Pulse_Width(x, PWM_SEQ[y][x]);
delay(1);
}
delay(300);
}
}
}
// -----------------------------------------------------------------------------------------------------
// only let one loop run at a time.
// comment out the other.
void loop()
{
if (1==1)
dance_loop();
else
precision_loop();
}
// -----------------------------------------------------------------------------------------------------
// End of File