Sean
Here is some (unfinished) code I threw together for you. Switch detection logic not tested, may have it backwards as its in an analog port. Code is shown below as well as attached. Use ROBOTC 3.51 as it uses pointers.
#pragma config(Sensor, in1, , sensorAnalog)
#pragma config(Sensor, dgtl1, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl2, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl3, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl4, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl5, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl6, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl7, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl8, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl9, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl10, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl11, , sensorLEDtoVCC)
#pragma config(Sensor, dgtl12, , sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*-----------------------------------------------------------------------------*/
/* */
/* lightshow.c */
/* 26 Oct 2012 */
/* James Pearman */
/* */
/*-----------------------------------------------------------------------------*/
// Various LED patterns
// Each bit corresponds to one LED
// 1 = on, 0 = off
//
short pattern_0] = {
0b000000000001,
0b000000000010,
0b000000000100,
0b000000001000,
0b000000010000,
0b000000100000,
0b000001000000,
0b000010000000,
0b000100000000,
0b001000000000,
0b010000000000,
0b100000000000,
0b010000000000,
0b001000000000,
0b000100000000,
0b000100000000,
0b000010000000,
0b000001000000,
0b000000100000,
0b000000010000,
0b000000001000,
0b000000000100,
0b000000000010
};
short pattern_1] = {
0b100000000001,
0b010000000010,
0b001000000100,
0b000100001000,
0b000010010000,
0b000001100000,
0b000001100000,
0b000010010000,
0b000100001000,
0b001000000100,
0b010000000010,
0b100000000001
};
short pattern_2] = {
0b101010101010,
0b010101010101,
0b000000000000,
0b101010101010,
0b010101010101,
0b000000000000,
0b101010101010,
0b010101010101,
0b000000000000,
0b101010101010,
0b010101010101,
0b000000000000,
0b101010101010,
0b010101010101,
0b000000000000
};
static short runState = 0;
static short runStateMax = 0; // set this in main when you have decided
// the number of patterns
/*-----------------------------------------------------------------------------*/
/* Button press to break sequence */
/* There is no digital port available so have to use an analog port */
/*-----------------------------------------------------------------------------*/
// Did not test this, may have logic backwards
short
CheckButton()
{
// check touch sensor in analog 1
if( SensorValue in1 ] < 100 )
{
// Wait for release
while( SensorValue in1 ] < 100 )
wait1Msec(25);
// increment a global to indicate the pattern to show
if(++runState > runStateMax)
runState = 0;
return(1);
}
else
return(0);
}
/*-----------------------------------------------------------------------------*/
/* Output one word to the 12 LEDs in the digital ports */
/*-----------------------------------------------------------------------------*/
// for debuging
short LedDebug[12];
void
display(short output)
{
int i;
for(i=0;i<12;i++)
{
SensorValue dgtl1 + i ] = output & 0x0001;
// Emulator does not show sensors so use a global to see what is happening
LedDebug i ] = output & 0x0001;
output >>= 1;
}
}
/*-----------------------------------------------------------------------------*/
/* Playback a pattern sequence */
/*-----------------------------------------------------------------------------*/
short
RunPattern( short *p, short len, short delayMs )
{
int i = 0;
for(i=0;i<len;i++)
{
display( *p++ );
wait1Msec(delayMs);
// button terminates early
if( CheckButton() )
return(0);
}
return(1);
}
/*-----------------------------------------------------------------------------*/
/* Create a random sequence */
/*-----------------------------------------------------------------------------*/
short
RunRandom( short timeMs, short delayMs )
{
int i;
// reseed based on current systen time
srand( nSysTime );
for(i=0;i<timeMs/delayMs;i++)
{
display( rand() % 4095 );
wait1Msec(delayMs);
// button terminates early
if( CheckButton() )
return(0);
}
return(1);
}
/*-----------------------------------------------------------------------------*/
/* Main task - run a silly lightshow on 12 LEDs connected to digital outputs */
/* 1 through 12 */
/*-----------------------------------------------------------------------------*/
task main()
{
int delay, i;
// we cycle 0 through 4
runStateMax = 4;
while(1)
{
// If runstate is 0 then we cycle through all patterns else just use one
// Pattern 1
if( (runState == 0) || (runState == 1) )
{
// run a pattern with speed 50
for(i=0;i<5;i++)
{
if( RunPattern( &pattern_0[0], sizeof(pattern_0)/sizeof(short), 50 ) == 0)
break;
}
}
// Pattern 2
if( (runState == 0) || (runState == 2) )
{
// gradually speeds up
for(delay=100;delay>10;delay-=10)
{
if( RunPattern( &pattern_1[0], sizeof(pattern_1)/sizeof(short), delay ) == 0)
break;
}
}
// Pattern 3
if( (runState == 0) || (runState == 3) )
{
// flashing
for(delay=100;delay>10;delay-=10)
{
if( RunPattern( &pattern_2[0], sizeof(pattern_2)/sizeof(short), delay ) == 0 )
break;
}
}
// Pattern 4
if( (runState == 0) || (runState == 4) )
{
// random pattern
RunRandom( 5000, 100 );
}
// No need for delay as delays are in display routines
}
}
Edit:
Updated version to fix bugs due to not actually testing the code this morning. Switch input finished (it was inverted earlier)
lightshow.c (7.1 KB)