SparkCore Garage Opener Code

// This #include statement was automatically added by the Spark IDE.

//*************************************//
// — WIDE.HK—//
// – SSD131x PMOLED Controller -//
// – SCL, SDA, GND, VCC(3.3v 5v) –//
//*************************************//

/*
****************************************
This code is rewritten from the original code for the Arduino to control the I2C OLED from http://Wide.HK

The code below works to use the SparkCore microcontrller (available at http://spark.io )
Pins used on the SparkCore:

DO – SDA
D1 – SCL
3V
GND

see here for pins: http://docs.spark.io/#/hardware/pins-and-i-o-i2c
****************************************
*/

char OLED_Address = 0x3c;
char OLED_Command_Mode = 0x80;
char OLED_Data_Mode = 0x40;

int digitalRelayPin = D2;
int digitalRelayGNDPin = D3;

int redLEDPin = D7;
int blueLEDPin = D6;

int doorOpenPin = A0;
int loopCount = 0;
int isOpen = 0;

void setup()
{

//screen = new WideOled();

//begin Wire communication with OLED
Wire.begin();

//set the one external function to call to update the OLED with a message
Spark.function(“update”, update);

// //initialize screen
SetupScreen();

pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(digitalRelayPin, OUTPUT);
pinMode(digitalRelayGNDPin, OUTPUT);
pinMode(doorOpenPin, INPUT);

//set GND pin for relay function
digitalWrite(digitalRelayGNDPin, LOW);

//function calls
Spark.function(“flash”, flash);
Spark.function(“turnLedOn”, turnLedOn);
Spark.function(“turnLedOff”, turnLedOff);

//variables
Spark.variable(“loopCount”, &loopCount, INT);
Spark.variable(“isOpen”, &isOpen, INT);

//flashLeds(3,50);

//wait to show the cyan indication of web access

sendCommand(0x01); // ** Clear display
sendMessage(“Smart Garage V1,started”);

delay(4000);

//take control of the RGB LED
RGB.control(true);

sendCommand(0x01); // ** Clear display

}

void loop()
{
//nothing happens in loop in this example
//just call ‘update’ from the web to update screen

/*

if(Spark.connected()){
// Sets the LED to blue
RGB.color(0, 0, 255);
}else{
// Set the LED to red
RGB.color(255, 0, 0);
}

isOpen = digitalRead(doorOpenPin);

if(!isOpen){
digitalWrite(redLEDPin, HIGH); // turn the LED on (HIGH is the voltage level)
}else{
digitalWrite(redLEDPin, LOW);
}

loopCount++;
delay(200);

*/

delay(1000);

}

int update(String args)
{

int status_code = 0;

sendCommand(0x01); // ** Clear display

//fix string
args.replace(“%20″,” “);
int commaPosition = args.indexOf(“,”);//find if there is a delim character

if(commaPosition>-1){
//two lines
sendMessage(args.substring(0,commaPosition));//send first part
sendCommand(0xC0); // ** New Line
sendMessage(args.substring(commaPosition+1, args.length()));//send second part

status_code = 2;//lines
}else{
//one line
sendMessage(args);
status_code = 1;//lines
}

//how many lines sent to display

return status_code;
}

//LED AND RELAY CODE
void flashLeds(int number, int spaceLength){
// Set the LED to green
RGB.color(0, 255, 0);

int index = 0;
while(indexCom0 Seg0->Seg99

// ** Set OLED Characterization * //
sendCommand(0x2A); // ** Set “RE”=1
sendCommand(0x79); // ** Set “SD”=1

// ** CGROM/CGRAM Management * //
sendCommand(0x72); // ** Set ROM
sendCommand(0x00); // ** Set ROM A and 8 CGRAM

sendCommand(0xDA); // ** Set Seg Pins HW Config
sendCommand(0x10);

sendCommand(0x81); // ** Set Contrast
sendCommand(0xFF);

sendCommand(0xDB); // ** Set VCOM deselect level
sendCommand(0x30); // ** VCC x 0.83

sendCommand(0xDC); // ** Set gpio – turn EN for 15V generator on.
sendCommand(0x03);

sendCommand(0x78); // ** Exiting Set OLED Characterization
sendCommand(0x28);
sendCommand(0x2A);
//sendCommand(0x05); // ** Set Entry Mode
sendCommand(0x06); // ** Set Entry Mode
sendCommand(0x08);
sendCommand(0x28); // ** Set “IS”=0 , “RE” =0 //28
sendCommand(0x01);
sendCommand(0x80); // ** Set DDRAM Address to 0x80 (line 1 start)

delay(100);
sendCommand(0x0C); // ** Turn on Display

}

void sendData(unsigned char data)
{
Wire.beginTransmission(OLED_Address); // ** Start I2C
Wire.write(OLED_Data_Mode); // ** Set OLED Data mode
Wire.write(data);
Wire.endTransmission(); // ** End I2C
}

void sendCommand(unsigned char command)
{
Wire.beginTransmission(OLED_Address); // ** Start I2C
Wire.write(OLED_Command_Mode); // ** Set OLED Command mode
Wire.write(command);
Wire.endTransmission(); // ** End I2C
delay(10);
}

void sendMessage(String message)
{
unsigned char i=0;
while(message[i])
{
sendData(message[i]); // * Show String to OLED
i++;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s