I am sending integers from max msp via serial into arduino and store them into variable name uint8_t buttonPressCount = 0;
function of receiving the variable
void controlSequenceScene() {
if (Serial.available() > 0) {
char id = Serial.read();
int buttonPressCount = Serial.parseInt();
if (id == scene_id) {
int buttonPressCount = Serial.parseInt();
Serial.print("sequence: ");
Serial.println(buttonPressCount);
}
}
}
then I have few function that should run if buttonPressCount is certain number:
for example:
if (buttonPressCount == 1)
{
Serial.println("Sequence 1 on");
currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
{
// Is this led active
if (mySolenoid[x].startTime < currentMillis)
{
// Do we need to calculate a next pulse time?
if (mySolenoid[x].nextPulseStop < currentMillis)
{
// Turn off led
digitalWrite(mySolenoid[x].pin, LOW);
Serial.print("solenoid: ");
Serial.print(x);
Serial.println("off");
// Calculate nextPulseStart & nextPulseStop
long startOffset = random(mySolenoid[x].minPulse, mySolenoid[x].maxPulse);
mySolenoid[x].nextPulseStart = startOffset + currentMillis;
mySolenoid[x].nextPulseStop = mySolenoid[x].nextPulseStart + mySolenoid[x].pulseLength;
}
// Are we currently in the middle of a pulse?
if (mySolenoid[x].nextPulseStart < currentMillis &&
mySolenoid[x].nextPulseStop > currentMillis)
{
//turn on solenoid
digitalWrite(mySolenoid[x].pin, HIGH);
Serial.print("solenoid: ");
Serial.print(x);
Serial.println("on");
}
}
}
}
}
and
//////****END SEQUENCE*****///////
void endSequence()
{
if (buttonPressCount >= 3)
{
// currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
{
// digitalWrite(EndPin[x], LOW);
digitalWrite(mySolenoid[x].pin, LOW);
}
}
}
the problem is that it seems the data I'm receiving via serial is not save into my "buttonPresscount" variable and therefore sequence are not happening
full code:
#define NUM_SOLENOID 9
//start program at x minute//
#define OFFSET 0 //in minutes
const char scene_id = 'H';
uint8_t buttonPressCount = 0; // Note an uint8_t stores a number from 0-255, so after 255 the buttonPressCount will go back to 0 again. If you want to store a larger integer, use uint16_t (0-65535), uint32_t (0-4294967295), or even uint64_t (0-18446744073709551615).
uint32_t millisTimePress = 0; // unsigned longs are uint32_t, an unsigned 32 bit integer.
uint32_t currentMillis;
//PULSE//
const uint16_t PULSE = 20;
//Solenoid structure//
struct Solenoid
{
unsigned long startTime; // The time this led is first active after a buttun is first pressed
unsigned long minPulse; // The minimum time the next pulse is generated
unsigned long maxPulse; // The maximum time the next pulse is generated
unsigned long nextPulseStart; // The time of the next pulse starts
unsigned long nextPulseStop; // The time of the next pulse stops
unsigned long pulseLength; // The on time of the pulse
uint8_t pin; // led GPIO pin number
};
// Start pulse min max
Solenoid mySolenoid[NUM_SOLENOID] = {
0 min, 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
3 min, 7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
5 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
9 min, 5 sec, 10 sec, 0, 0, PULSE, 3, //solenoid num 4
11.5 min, 5 sec, 8 sec, 0, 0, PULSE, 8,//solenoid num 5
13 min, 5 sec, 8 sec, 0, 0, PULSE, 4, // num 6
15.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7, // num 7
17.5 min, 3 sec, 9 sec, 0, 0, PULSE, 5, // num 8
18.5 min, 2 sec, 6 sec, 0, 0, PULSE, 6 // num 9
};
///middle solenoid struct//
struct SolenoidMiddle
{
unsigned long startTime; // The time this led is first active after a buttun is first pressed
unsigned long stopTime; // The time this led becomes inactive after a buttun is second time pressed
unsigned long minPulse; // The minimum time the next pulse is generated
unsigned long maxPulse; // The maximum time the next pulse is generated
unsigned long nextPulseStart; // The time of the next pulse starts
unsigned long nextPulseStop; // The time of the next pulse stops
unsigned long pulseLength; // The on time of the pulse
uint8_t pin; // led GPIO pin number
};
// start end minpulse maxpulse
SolenoidMiddle mySolenoidMiddle[NUM_SOLENOID] = {
0 sec, 100 min , 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
0 sec, 1.5 min, 7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
0 sec, 3 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 3, //4
0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 8, //5
0 sec, 5.5 min, 5 sec, 8 sec, 0, 0, PULSE, 4, //6
0 sec, 7.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7, //7
0 sec, 4.5 min, 6 sec, 11 sec, 0, 0, PULSE, 5, //8
0 sec, 9 min, 2 sec, 6 sec, 0, 0, PULSE, 6 //9
};
// millis function for offseting (debug from a specific time)
uint32_t myMillis()
{
return millis() + (OFFSET * 60 * 1000UL);
}
void setup()
{
Serial.setTimeout(10);
Serial.begin(115200);
// Setup the button.
pinMode(switchPin, INPUT_PULLUP);
// Setup the LED.
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Solenoid
for (uint8_t i = 0; i < NUM_SOLENOID; i++)
{
pinMode(mySolenoid[i].pin, OUTPUT);
}
}
void loop()
{
controlSequenceScene();
startSequence();
middleSequence();
endSequence();
}
void controlSequenceScene() {
if (Serial.available() > 0) {
char id = Serial.read();
int buttonPressCount = Serial.parseInt();
if (id == scene_id) {
int buttonPressCount = Serial.parseInt();
Serial.print("sequence: ");
Serial.println(buttonPressCount);
}
}
}
//////****START SEQUENCE*****///////
void startSequence()
{
if (buttonPressCount == 1)
{
Serial.println("Sequence 1 on");
currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
{
// Is this led active
if (mySolenoid[x].startTime < currentMillis)
{
// Do we need to calculate a next pulse time?
if (mySolenoid[x].nextPulseStop < currentMillis)
{
// Turn off led
digitalWrite(mySolenoid[x].pin, LOW);
Serial.print("solenoid: ");
Serial.print(x);
Serial.println("off");
// Calculate nextPulseStart & nextPulseStop
long startOffset = random(mySolenoid[x].minPulse, mySolenoid[x].maxPulse);
mySolenoid[x].nextPulseStart = startOffset + currentMillis;
mySolenoid[x].nextPulseStop = mySolenoid[x].nextPulseStart + mySolenoid[x].pulseLength;
}
// Are we currently in the middle of a pulse?
if (mySolenoid[x].nextPulseStart < currentMillis &&
mySolenoid[x].nextPulseStop > currentMillis)
{
//turn on solenoid
digitalWrite(mySolenoid[x].pin, HIGH);
Serial.print("solenoid: ");
Serial.print(x);
Serial.println("on");
}
}
}
}
}
//////****MIDDLE SEQUENCE*****///////
void middleSequence()
{
if (buttonPressCount == 2)
{
currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
{
// Is this led active
if (mySolenoidMiddle[x].startTime < currentMillis && mySolenoidMiddle[x].stopTime > currentMillis)
{
// Do we need to calculate a next pulse time?
if (mySolenoidMiddle[x].nextPulseStop < currentMillis)
{
// Turn off led
digitalWrite(mySolenoidMiddle[x].pin, LOW);
// Calculate nextPulseStart & nextPulseStop
long startOffset = random(mySolenoidMiddle[x].minPulse, mySolenoidMiddle[x].maxPulse);
mySolenoidMiddle[x].nextPulseStart = startOffset + currentMillis;
mySolenoidMiddle[x].nextPulseStop = mySolenoidMiddle[x].nextPulseStart + mySolenoidMiddle[x].pulseLength;
}
// Are we currently in the middle of a pulse?
if (mySolenoidMiddle[x].nextPulseStart < currentMillis &&
mySolenoidMiddle[x].nextPulseStop > currentMillis)
{
//turn on led
digitalWrite(mySolenoidMiddle[x].pin, HIGH);
}
}
}
}
}
//////****END SEQUENCE*****///////
void endSequence()
{
if (buttonPressCount >= 3)
{
// currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
{
// digitalWrite(EndPin[x], LOW);
digitalWrite(mySolenoid[x].pin, LOW);
}
}
}
