Skip to content

Instantly share code, notes, and snippets.

@mbparks
Created September 17, 2014 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbparks/46decd287d7d9f07534e to your computer and use it in GitHub Desktop.
Save mbparks/46decd287d7d9f07534e to your computer and use it in GitHub Desktop.
/* BT_Lights Demo App for Use With MIT App Inventor
This sketch is forked from a demo sketch written by Steve Chang
This demo code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <SoftwareSerial.h> //Software Serial Port
#include <avr/pgmspace.h>
#define DATA_1 (PORTC |= 0X01) // DATA 1 // for UNO
#define DATA_0 (PORTC &= 0XFE) // DATA 0 // for UNO
#define STRIP_PINOUT (DDRC=0xFF) // for UNO
#define RxD 6
#define TxD 7
#define LED 13
#define DEBUG_ENABLED 1
String valRead = "";
uint32_t intensityR = 0;
uint32_t intensityG = 0;
uint32_t intensityB = 0;
uint32_t overallIntensity = 0;
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(LED, OUTPUT);
setupBlueToothConnection();
STRIP_PINOUT;
reset_strip();
}
void loop()
{
char recvChar;
while(1){
if(blueToothSerial.available()) {
recvChar = blueToothSerial.read();
switch (recvChar) {
case 'R':
Serial.print("RED: ");
valRead = blueToothSerial.readStringUntil('\n');
//valRead.trim();
intensityR = valRead.toInt();
Serial.println(intensityR);
overallIntensity = (intensityB<<8)|(intensityG<<16)|intensityR;
for (int x = 0; x<10; x++) {
send_strip(overallIntensity);
}
break;
case 'G':
Serial.print("GREEN: ");
valRead = blueToothSerial.readStringUntil('\n');
//valRead.trim();
intensityG = valRead.toInt();
Serial.println(intensityG);
overallIntensity = (intensityB<<8)|(intensityG<<16)|intensityR;
for (int x = 0; x<10; x++) {
send_strip(overallIntensity);
}
break;
case 'B':
Serial.print("BLUE: ");
valRead = blueToothSerial.readStringUntil('\n');
//valRead.trim();
intensityB = valRead.toInt();
Serial.println(intensityB);
overallIntensity = (intensityB<<8)|(intensityG<<16)|intensityR;
for (int x = 0; x<10; x++) {
send_strip(overallIntensity);
}
break;
}
Serial.println(overallIntensity);
}
if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=BTLights\r\n"); //set the bluetooth name as "BTLights"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
/*******************************************************************************
* Function Name : send_strip
* Description : Transmit 24 pulse to LED strip
*
* Input : 24-bit data for the strip
*
* Output : None
* Return : None
*******************************************************************************/
void send_strip(uint32_t data)
{
int i;
unsigned long j=0x800000;
for (i=0;i<24;i++)
{
if (data & j)
{
DATA_1;
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
/*----------------------------*/
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
/*----------------------------*/
DATA_0;
}
else
{
DATA_1;
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
DATA_0;
/*----------------------------*/
__asm__("nop\n\t");
__asm__("nop\n\t");
__asm__("nop\n\t");
/*----------------------------*/
}
j>>=1;
}
}
/*******************************************************************************
* Function Name : reset_strip
* Description : Send reset pulse to reset all color of the strip
*
* Input : None
*
* Output : None
* Return : None
*******************************************************************************/
void reset_strip()
{
DATA_0;
delayMicroseconds(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment