This has had me stumped for a couple of hours. The function CUBEPowerUp(), which works in other sketches, (including those running it in Setup, like this one) is not being run. I do get the message indicating that Setup has finished.
Note: while trouble-shooting, I've eliminated the main loop to avoid the sketch running it.
For comparison I've also shown the code of a similar sketch that works fine.
// Friday 13 November 2020, 1746. NOT POWERING UP
// Using pin 10 (green LED) to indicate D6 trigger tasks in progress
// Pin 4 input sets video duration (H = 30s, L = 5s))
// Pin 6 input LOW triggers loop
// Pin 8 HIGH = CUBE video recording
// Pin 9 is CUBE servo control
// Pin 10 is greenledPin
#include <VarSpeedServo.h> // Uses the VarSpeedServo library
// Create servo object
VarSpeedServo myCUBEservo;
const byte outPosCUBE = 35; // Trial & error; needs regular re-checking
const byte inPosCUBE = 15; // Ditto
const byte triggerPin = 6;
const byte redledPin = 8;
const byte greenledPin = 10; // (Green LED on bb)
const byte switchPin = 4;
int videoLength; // Declared here, then set in Setup
// From 'Basic-goTime' sketch
int nextTime = 20000; // Do this every 20s
long int goTime;
void setup()
{
// Power up CUBE
CUBEPowerUp();
delay(2500);
Serial.println("CUBE powered up and recovery delay ended");
Serial.begin(115200);
Serial.println("");
Serial.println("CUBE-VideosPhotosKA-13Nov20");
pinMode(switchPin, INPUT_PULLUP);
pinMode(triggerPin, INPUT_PULLUP);
pinMode(redledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
// Initialise the CUBE servo
myCUBEservo.write(outPosCUBE); // Initial arm pos'n
myCUBEservo.attach(9); // White wires
Serial.println("CUBE servo attached");
// Test D4 to choose value of videolength
if (digitalRead(switchPin) == HIGH)
{
videoLength = 30000;
}
else
{
videoLength = 8000;
}
Serial.print("Video length = ");
Serial.println(videoLength);
Serial.print("outPosCUBE/inPosCUBE = ");
Serial.print(outPosCUBE);
Serial.print("/");
Serial.println(inPosCUBE);
Serial.println("Setup finished; waiting for low D6");
Serial.println("--------------------------------------------");
// From 'Basic-goTime' sketch
goTime = millis(); // Start time
}
// Had to include this to avoid error about undefined reference to 'loop'
void loop() {}
void CUBEPowerUp()
{
// Power up CUBE with long press > 3 s
myCUBEservo.write(inPosCUBE);
delay(3500); // Press for > 3s to toggle power
myCUBEservo.write(outPosCUBE);
delay(200);
}
void CUBESingleButtonPress()
{
// Start or Stop video with single button press
myCUBEservo.write(inPosCUBE);
delay(200);
myCUBEservo.write(outPosCUBE);
delay(200);
}
void functionGo()
{
// Take a photo with CUBE: single press
CUBESingleButtonPress();
goTime = millis() + nextTime; // When to do it again
}
But the following works OK:
// Friday 13 November 2020, 15:13, house;
// Using pin 10 (green LED) to indicate D6 trigger tasks in progress
// Pin 4 input sets video duration (H = 30s, L = 5s))
// Pin 6 input LOW triggers loop
// Pin 8 HIGH = CUBE video recording
// Pin 9 is CUBE servo control
#include <VarSpeedServo.h> // Uses the VarSpeedServo library
// Create servo object
VarSpeedServo myCUBEservo;
const byte outPosCUBE = 35; // Trial & error; needs regular re-checking
const byte inPosCUBE = 15; // Ditto
const byte triggerPin = 6;
const byte redledPin = 8;
const byte greenledPin = 10; // (Green LED on bb)
const byte switchPin = 4;
int videoLength; // Declared here, then set in Setup
void setup()
{
Serial.begin(115200);
Serial.println("");
Serial.println("CUBE-Videos-13Nov20");
pinMode(switchPin, INPUT_PULLUP);
pinMode(triggerPin, INPUT_PULLUP);
pinMode(redledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
// Initialise the CUBE servo
myCUBEservo.write(outPosCUBE); // Initial arm pos'n
myCUBEservo.attach(9); // White wires
Serial.println("CUBE servo attached");
// Test D4 to choose value of videolength
if (digitalRead(switchPin) == HIGH)
{
videoLength = 30000;
}
else
{
videoLength = 8000;
}
Serial.print("Video length = ");
Serial.println(videoLength);
Serial.print("outPosCUBE/inPosCUBE = ");
Serial.print(outPosCUBE);
Serial.print("/");
Serial.println(inPosCUBE);
Serial.println("Setup finished; waiting for low D6");
Serial.println("--------------------------------------------");
}
void loop()
{
// Run only when a LOW on D6 is detected
if (digitalRead(triggerPin) == LOW)
{
Serial.println("Triggered by D6; TASKS STARTED");
// TASK #1: Take greenledPin (D10) high, to indicate D6 trigger has occurred
digitalWrite(greenledPin , HIGH);
// TASK #2: Power up CUBE
CUBEPowerUp();
// Seems to then need a long delay - for battery recovery?
delay(2500); // 1500 & 2500 worked; 500 & 1000 failed
Serial.println("CUBE powered up and recovery delay ended");
// TASK #3: Start CUBE video with two presses
CUBESingleButtonPress();
delay(10);
// TASK #4: Start video with two presses
CUBESingleButtonPress();
Serial.println("Started CUBE video");
digitalWrite(redledPin , HIGH) ;
// TASK #5: Delay for recording videos
delay(videoLength); // ACTUAL individual durations will be different
// TASK #6: Stop CUBE video with single press
CUBESingleButtonPress();
digitalWrite(redledPin , LOW) ;
Serial.println("CUBE video ended");
// TASK #7: Power down CUBE
delay(2000);
CUBEPowerDown();
// TASK #8: Take greenledPin (D10) low, to indicate triggered task finished
digitalWrite(greenledPin , LOW);
delay(2000); // Brief pause while CUBE powering down, may be redundant
}
}
// Servo functions here; take care when updating
// *********************************************
void CUBEPowerUp()
{
// Power up CUBE with long press > 3 s
myCUBEservo.write(inPosCUBE);
delay(3500); // Press for > 3s to toggle power
myCUBEservo.write(outPosCUBE);
delay(200);
}
void CUBEPowerDown()
{
// Power down CUBE with long press > 3 s
myCUBEservo.write(inPosCUBE);
delay(3500); // Press for > 3s to power down
myCUBEservo.write(outPosCUBE);
delay(1000);
// CUBE gives a final longish beep before switching off
Serial.println("Cube powered down; TASKS ENDED");
Serial.println(" "); // Blank line
}
void CUBESingleButtonPress()
{
// Stop video with single button press
myCUBEservo.write(inPosCUBE);
delay(200);
myCUBEservo.write(outPosCUBE);
delay(200);
}