Hello,
I’ve been working on this project for quite some time now but i cant seem to get my servo’s to move slower and the lights fading.
The servo’s just jumps to position in the same speed and the leds just go on and of.
I’m using an arduino micro witch I tested with the blink without delay and fading sketches, they both do what they are supposed to do.
I think I have the fading without delay correct, and the servo stuff as well. But i cant for the life of me get it to work.
Here is my code;
- the digitalwrite on pin 13 is just for testing purposes.
- also there is some stuff in there that is not worked out jet.
#include <SPI.h> //bluetooth
#include "Adafruit_BLE_UART.h" //bluetooth
#include <SoftwareSerial.h> //sounboard
#include "Adafruit_Soundboard.h" //sounboard
#include <Wire.h> //servodriver
#include <Adafruit_PWMServoDriver.h> //servodriver
//---------------------------Lights---------------------------------------------------------
// will store last time LED was updated
long previousMillisFading = 0;
// intervals
long interval1 = 50; // interval at which to fade in (milliseconds)
long interval2 = 40; // interval at which to fade out (milliseconds)
//led states
int ledblue = 0; // tells if the blue leds are on or off
int ledred = 0; // tells if the red leds are on or off
int fadeValue1 = 0; // fadeValues used to set the blue LED
int fadeValue2 = 0; // fadeValues used to set the Red LED
//---------------------------Bluetooth---------------------------------------------------------
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10 //DONT CHANGE THESE PINS, IT WILL FUCK UP YOUR ARDUINO MICRO/BLUETOOTH
#define ADAFRUITBLE_RDY 7 //DONT CHANGE THESE PINS, IT WILL FUCK UP YOUR ARDUINO MICRO/BLUETOOTH
#define ADAFRUITBLE_RST 9 //DONT CHANGE THESE PINS, IT WILL FUCK UP YOUR ARDUINO MICRO/BLUETOOTH
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
//---------------------------soundboard---------------------------------------------------------
// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX 1
#define SFX_RX 0
// Connect to the RST pin on the Sound Board
#define SFX_RST 4
// we'll be using software serial
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
// pass the software serial to Adafruit_soundboard, the second
// argument is the debug port (not used really) and the third
// arg is the reset pin
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
// can also try hardware serial with
// Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);
//---------------------------Servos---------------------------------------------------------
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you have!
//BACK
#define SERVOMIN 137 // this is the 'minimum' pulse length count of the back servo's (out of 4096) Depends on your servo, DONT CHANGE THESE FUCKERS!!!!
#define SERVOMAX 660 // this is the 'maximum' pulse length count of the back servo's (out of 4096) Depends on your servo, DONT CHANGE THESE FUCKERS!!!!
//TOP
#define SERVOMIN1 230 // this is the 'minimum' pulse length count of the faceplate servo's (out of 4096) Depends on your servo, DONT CHANGE THESE FUCKERS!!!!
#define SERVOMAX1 520 // this is the 'maximum' pulse length count of the faceplate servo's (out of 4096) Depends on your servo, DONT CHANGE THESE FUCKERS!!!!
long interval = 150; // interval at which to fade in (milliseconds)
long previousMillisServo = 0;
int pulselen = 0;
//---------------------------Other---------------------------------------------------------
int counterintro = 0;
int counterservo = 0;
int countermotor = 0;
//-----------------------------------------------------------------------------------------
void setup(void)
{
ss.begin(9600); // softwareserial at 9600 baud
while (!Serial1); // Leonardo/Micro should wait for serial init
BTLEserial.setDeviceName("MK42"); /* 7 characters max! */
BTLEserial.begin();
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz update
yield();
pinMode(A0, OUTPUT); //motordriver
pinMode(A1, OUTPUT); //motordriver
pinMode(A2, OUTPUT); //motordriver
pinMode(A3, OUTPUT); //motordriver
pinMode(5, OUTPUT); //Blue Leds (PWM)
pinMode(6, OUTPUT); //Red Leds (PWM)
pinMode(12, OUTPUT); //EL Wire
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
if (status == ACI_EVT_DEVICE_STARTED) {
counterintro = 0;
}
if (status == ACI_EVT_CONNECTED) {
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
if (c == '1') {
Open();
}
if (c == '2') {
Close();
}
if (c == '3') {
normalmode();
}
if (c == '4') {
battlemode();
}
if (c == '5') {
off();
}
}
String i/*ntro*/ = ("MK42 connected\n open = 1\n close = 2\n battle mode = 3\n normal mode = 4\n Off = 5\n");
// We need to convert the line to bytes, no more than 20 at this time
uint8_t sendbuffer[80];
i.getBytes(sendbuffer, 80);
char sendbuffersize = min(80, i.length());
if (counterintro < 1) {
// write the data
BTLEserial.write(sendbuffer, sendbuffersize);
counterintro++;
}
}
if (status == ACI_EVT_DISCONNECTED) {
}
}
//------------------------------------------------------------------------------------------------------------------
void Open () {
if (millis() - previousMillisServo > interval) {
previousMillisServo = millis();
for (pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
pwm.setPWM(servonum, 0, pulselen);
}
}
digitalWrite(13, HIGH);
}
//------------------------------------------------------------------------------------------------------------------
void Close () {
pwm.setPWM(15, 0, SERVOMIN1 );
digitalWrite(13, LOW);
}
//------------------------------------------------------------------------------------------------------------------
void battlemode() {
if (millis() - previousMillisFading > interval2) {
previousMillisFading = millis();
if ( fadeValue2 < 255 ) fadeValue2 += 5;
if ( fadeValue1 <= 255 ) fadeValue1 -= 5;
}
analogWrite(6, fadeValue2);
analogWrite(5, fadeValue1);
// if (fadeValue2 == 255) ledred = 1;
// if (fadeValue1 == 0) ledblue = 0;
}
If you guys could give me some pointers i’ll be happy
(sorry for my bad englisch)