yourwhyphy:
blackfin!!!
it worked, thank you so much.
Would it be the same concept if i wanted to make each one of those pins stay on for their own assigned time instead of having pin 7,9 and 11,13 go together, have each pin 7,9,11,13 turn off on a set time i assign? basically now i want to do this but do each independently. like pin 7 1100ms, pin 9 1200ms, pin 11, 1300ms, and pin 13 1400ms.
Absolutely. The code here (should!) do that. Note that we could use the same model, just using 4 bits in the flag byte and add additional timer variables for each pin etc, I'll use a different tactic here.
Because the pins share very similar behaviors and characteristics, I've created a structure and made it a type. I then create an array of this type, one member for each pin. Each pin then has its own flag, timer, delay value etc. We can use for loops (or other methods) to check each pin's state and timing requirements independently of the others. And because it's an array we're checking the code is ends up being a bit neater and tidier.
The code might look a little weird compared to the first code but if you walk through it you should be able to see what it's doing.
Once again: Compiles, not tested.
#define NUM_PINS 4
char
Incoming_value = 0;
uint32_t
timeNow;
//because we're doing similar actions on multiple pins, we
//can create a structure and denote it a "type", just like
//char or int etc. This way, we can create an array of this type
//specifically to service a number of pins that have similar
//behaviors
typedef struct
{
bool flag; //the flag tells us if we're timing a high-state (true=timing)
uint8_t pin; //the pin number
uint32_t timePin; //time time the pin went high (millis)
uint32_t timeDelay; //the time we want this pin to stay high
}PinControl_t;
//here's the array of size 4 (the number of pins: 7, 9, 11 and 13).
//we can initialize it with parameters we know now:
PinControl_t pinControl[NUM_PINS] =
{
{
//for pin 7:
.flag = false, //pin's not timing right now
.pin = 7, //pin number
.timePin = 0ul,
.timeDelay = 1100ul //how long we want it to stay on
},
{
//repeat for pins 9, 11 and 13
.flag = false,
.pin = 9,
.timePin = 0ul,
.timeDelay = 1200ul //1200mS for pin 9
},
{
.flag = false,
.pin = 11,
.timePin = 0ul,
.timeDelay = 1300ul //1300mS for pin 11
},
{
.flag = false,
.pin = 13,
.timePin = 0,
.timeDelay = 1400ul //1400mS for pin 13
}
};
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
}//setup
void loop()
{
timeNow = millis();
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
switch( Incoming_value )
{
case '0':
case '1':
//this form writes a HIGH if Incoming_value is equal to '1', otherwise a LOW
digitalWrite( 13, (Incoming_value == '1') ? HIGH:LOW );
break;
case '2':
case '3':
digitalWrite( 12, (Incoming_value == '3') ? HIGH:LOW );
break;
case '4':
case '5':
digitalWrite( 11, (Incoming_value == '5') ? HIGH:LOW );
break;
case '6':
case '7':
digitalWrite( 10, (Incoming_value == '7') ? HIGH:LOW );
break;
case '8':
case '9':
digitalWrite( 9, (Incoming_value == '9') ? HIGH:LOW );
break;
case 'A':
case 'B':
digitalWrite( 8, (Incoming_value == 'A') ? HIGH:LOW );
break;
case 'C':
case 'D':
digitalWrite( 7, (Incoming_value == 'C') ? HIGH:LOW );
break;
case 'E':
case 'F':
digitalWrite( 6, (Incoming_value == 'E') ? HIGH:LOW );
break;
case 'h':
//when we get an 'h', set all four pins high and set their flags and timers
for( uint8_t i=0; i<NUM_PINS; i++ )
{
digitalWrite( pinControl[i].pin, HIGH ); //pins high
pinControl[i].flag = true; //flags set
pinControl[i].timePin = timeNow; //grab the time
}//for
break;
}//switch
}//if serial available
//each pass through loop, check the flags to see if any pins' timers need attention
for( uint8_t idx=0; idx<NUM_PINS; idx++ )
{
//is this pin's flag set?
if( pinControl[idx].flag == true )
{
//yes, so pin is still high. is it time to put it low?
if( (timeNow - pinControl[idx].timePin) >= pinControl[idx].timeDelay )
{
//yes...clear the pin and its flag
digitalWrite( pinControl[idx].pin, LOW );
pinControl[idx].flag = false;
}//if
}//if
}//for
}//loop