Control De Andon Con Arduino Por Serial
Enviado por Arduino123 • 11 de Marzo de 2015 • 3.144 Palabras (13 Páginas) • 389 Visitas
//#define _DEBUG
enum SwitchMode
{
Slow,
Normal,
Fast
};
enum LoopMode
{
SingleShot,
Bounce,
Loop
};
enum Pins
{
Red = 0,
Orange,
Green,
Blue
};
const int pTelephone = 15;
void DebugPrint( char* _strInfo )
{
#ifdef _DEBUG
Serial.println( _strInfo );
#endif
}
void DebugPrint( int _nInfo )
{
#ifdef _DEBUG
Serial.print( _nInfo );
Serial.print( ", " );
#endif
}
// The analogue have 17 values due to inverse square law on led brighness
static const byte MAX_INDEX = 16;
static const byte s_fadeValues[] = { 255, 180, 128, 90, 64, 45, 32, 23, 16, 12, 8, 6, 4, 3, 2, 1, 0 };
static const byte s_slowBlinkValues[] = { 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static const byte s_fastBlinkValues[] = { 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0 };
// the colors are red, orange, green and blue respectively
// serial light instructions
static const byte s_onCommand[] = { '!', '@', '#', '$' };
static const byte s_offCommand[] = { '1', '2', '3', '4' };
// output pins
static const byte s_pins[] = { 3, 5, 6, 9 };
// values (off)
volatile byte g_values[] = { MAX_INDEX, MAX_INDEX, MAX_INDEX, MAX_INDEX };
// step
volatile byte g_step[] = { 0, 0, 0, 0 };
// light operation modes
volatile SwitchMode g_eSwitchMode[] = { Normal, Normal, Normal, Normal };
volatile SwitchMode g_eCurrentSwitchMode = Normal;
volatile LoopMode g_eLoopMode[] = { SingleShot, SingleShot, SingleShot, SingleShot };
volatile LoopMode g_eCurrentLoopMode = SingleShot;
void setup( )
{
Serial.begin( 9600 );
// make it output
pinMode( s_pins[ Red ], OUTPUT );
pinMode( s_pins[ Orange ], OUTPUT );
pinMode( s_pins[ Green ], OUTPUT );
pinMode( s_pins[ Blue ], OUTPUT );
/*
pinMode( pTelephone, INPUT );
digitalWrite( pTelephone, LOW );
pinMode( 14, INPUT );
digitalWrite( 14, LOW );
pinMode( 20, INPUT );
digitalWrite( 20, LOW );
*/
// wait to make sure serial is initialized on the host
delay(1000);
DebugPrint( "initialization done, starting demo mode" );
demoMode( );
}
void loop( )
{
byte idx;
byte nCurrentPinValue;
// check if data has been sent from the computer:
if (Serial.available( ))
{
byte input;
// read the most recent byte (which will be from 0 to 255):
input = Serial.read( );
switch ( input )
{
// switch mode
case 's':
case 'S':
DebugPrint( "switching to slow mode" );
g_eCurrentSwitchMode = Slow;
break;
case 'n':
case 'N':
DebugPrint( "switching to normal mode" );
g_eCurrentSwitchMode = Normal;
break;
case 'f':
case 'F':
DebugPrint( "switching to fast mode" );
g_eCurrentSwitchMode = Fast;
break;
// loop mode
case 'o':
case 'O':
DebugPrint( "switching to single shot mode" );
g_eCurrentLoopMode = SingleShot; // aka once
break;
case 'b':
case 'B':
DebugPrint( "switching to bounce mode" );
g_eCurrentLoopMode = Bounce;
break;
case 'l':
case 'L':
DebugPrint( "switching to loop mode" );
g_eCurrentLoopMode = Loop;
break;
// version
case 'v':
case 'V':
case 'h':
case 'H':
Serial.println("xopr's Andon Light v0.2");
Serial.println("");
Serial.println(" Usage:");
Serial.println(" [snf]?[obl]?([1234]|[!@#$])");
Serial.println("");
Serial.println(" Switch modes:");
Serial.println(" [s]low");
Serial.println(" [n]ormal");
Serial.println(" [f]ast");
Serial.println("");
Serial.println(" Loop modes:");
Serial.println(" [o]ne shot");
Serial.println(" [b]bounce");
Serial.println(" [l]loop");
Serial.println("");
Serial.println(" Lights:");
Serial.println(" {1!}: Red");
Serial.println(" {2@}: Orange");
Serial.println(" {3#}: Green");
Serial.println(" {4$}: Blue");
break;
// assume it's a switch command
default:
for ( idx = 0; idx < 4; idx++ )
{
if ( input == s_onCommand[ idx ] )
{
// apply the optionally prefixed or default modes
applyModes( idx );
// set the stepping, so we know that we need to update the output port
g_step[ idx ] = -1;
...