Need help programming for Pin 4 output to Android via BT

My son has a school project and I think he bit off more than he could chew, and this part is due Wednesday. He has a micro switch attached to an Arduino UNO board. He also has a Bluefruit LE - Bluetooth Low Energy (BLE 4.0) - nRF8001 that he is communicating to his smart phone with. When the switch is closed pin 4 goes High on the phone app so that part is working.

Now he just needs a message sent to the phone when pin4 goes high. Can anyone help him finish this?

Thank you

Just to be clear, his team (9th grade) has done everything up to this point but lack the programming skills to finish the final piece. If someone can help for a small cost that's fine or if someone could point him in a specific direction that would help too. Going through these sites is like drinking from a fire hydrant.

Could you explain some details about the app? What does "pin 4 goes High on the phone app" mean? I assume that you downloaded the app from somewhere and cannot build it yourself.

The Bluefruit tells the app that pin 4 is HIGH but you also want to send some text message, is that it? What would the message say? What would happen if the message was sent? I'm having a difficult time picturing what is going on and what you want to happen.

Could you post the Arduino sketch that your son is using?

Thanks for the reply.

We are using the example sketch Firmata_nRF8001. It is a very simple idea. The micro switch opens when a small door is opened. He wants to be able to send a message to a phone when that door is opened. With the canned sketch, he can see pin 4 change state when the door is open/closed.

He wants something like "Door is Open" to display on phone. Right now with the Bluefruit LE app he can see pin 4 go high/low when the door is closed/open

Thanks in you need the code posted here?

If you have a Firmata sketch installed, you've relinquished control over the Arduino completely. You can't make it do more stuff. It would be up to the phone app to add more functionality, like sending itself a text message.

I think the section of code we need is…this is from the Firmata_nRF8001 sketch

/* -----------------------------------------------------------------------------

  • check all the active digital inputs for change of state, then add any events
  • to the Serial output queue using () /
    void checkDigitalInputs(boolean forceSend = false)
    Using non-looping code allows constants to be given to readPort().
  • The compiler will apply substantial optimizations if the inputs
  • to readPort() are compile-time constants. /
    for (uint8_t i=0; i<TOTAL_PORTS; i++) {
    if (reportPINs
    ) {*
    _ // FIRMATADEBUG.print(“Reporting on port “); FIRMATADEBUG.print(i); FIRMATADEBUG.print(” mask 0x”); FIRMATADEBUG.println(portConfigInputs*, HEX);_
    uint8_t x = BLE_Firmata.readPort(i, portConfigInputs);
    _ // FIRMATADEBUG.print(“Read 0x”); FIRMATADEBUG.println(x, HEX);
    outputPort(i, x, forceSend);
    // -----------------------------------------------------------------------------
    / sets the pin mode to the correct state and sets the relevant bits in the_

    * two bit-arrays that track Digital I/O and PWM status
    void setPinModeCallback(byte pin, int mode)
    * //FIRMATADEBUG.print(“Setting pin #”); FIRMATADEBUG.print(pin); FIRMATADEBUG.print(" to "); FIRMATADEBUG.println(mode);*
    * if ((pinConfig[pin] == I2C) && (isI2CEnabled) && (mode != I2C)) {*
    * // disable i2c so pins can be used for other functions*
    * // the following if statements should reconfigure the pins properly*
    * disableI2CPins();*
    * }*
    * if (BLE_Firmata.IS_PIN_SERVO(pin) && mode != SERVO && servos[BLE_Firmata.PIN_TO_SERVO(pin)].attached()) {
    _ }_
    if (BLE_Firmata.IS_PIN_ANALOG(pin)) {
    reportAnalogCallback(BLE_Firmata.PIN_TO_ANALOG(pin), mode == ANALOG ? 1 : 0); // turn on/off reporting*

    * }*
    * if (BLE_Firmata.IS_PIN_DIGITAL(pin)) {
    _ if (mode == INPUT) {
    portConfigInputs[pin/8] |= (1 << (pin & 7));
    } else {
    portConfigInputs[pin/8] &= ~(1 << (pin & 7));
    // FIRMATADEBUG.print(F(“Setting pin #”)); FIRMATADEBUG.print(pin); FIRMATADEBUG.print(F(" port config mask to = 0x"));
    // FIRMATADEBUG.println(portConfigInputs[pin/8], HEX);
    pinState[pin] = 0;
    switch(mode) {
    case ANALOG:_
    if (BLE_Firmata.IS_PIN_ANALOG(pin)) {
    _ //FIRMATADEBUG.print(F(“Set pin #”)); FIRMATADEBUG.print(pin); FIRMATADEBUG.println(F(" to analog"));_
    if (BLE_Firmata.IS_PIN_DIGITAL(pin)) {
    pinMode(BLE_Firmata.PIN_TO_DIGITAL(pin), INPUT); // disable output driver*

    * }*
    * pinConfig[pin] = ANALOG;*
    * lastAnalogReads[BLE_Firmata.PIN_TO_ANALOG(pin)] = -1;
    _ }
    case INPUT:_
    if (BLE_Firmata.IS_PIN_DIGITAL(pin)) {
    _ //FIRMATADEBUG.print(F(“Set pin #”)); FIRMATADEBUG.print(pin); FIRMATADEBUG.println(F(" to input"));*_

pinMode(BLE_Firmata.PIN_TO_DIGITAL(pin), INPUT_PULLUP); // disable output driver*

* } else {*
* pinMode(BLE_Firmata.PIN_TO_DIGITAL(pin), INPUT); // disable output driver*
* }*
* pinConfig[pin] = INPUT;*

* // force sending state immediately*
* //delay(10);*
* //checkDigitalInputs(true); *
* }*
* break;*
* case OUTPUT:*
* if (BLE_Firmata.IS_PIN_DIGITAL(pin)) {
_ //FIRMATADEBUG.print(F(“Set pin #”)); FIRMATADEBUG.print(pin); FIRMATADEBUG.println(F(" to output"));_
digitalWrite(BLE_Firmata.PIN_TO_DIGITAL(pin), LOW); // disable PWM*

* pinMode(BLE_Firmata.PIN_TO_DIGITAL(pin), OUTPUT);
_ pinConfig[pin] = OUTPUT;
case PWM:_
if (BLE_Firmata.IS_PIN_PWM(pin)) {
_ //FIRMATADEBUG.print(F(“Set pin #”)); FIRMATADEBUG.print(pin); FIRMATADEBUG.println(F(" to PWM"));_
pinMode(BLE_Firmata.PIN_TO_PWM(pin), OUTPUT);
analogWrite(BLE_Firmata.PIN_TO_PWM(pin), 0);
_ pinConfig[pin] = PWM;
case SERVO:_
if (BLE_Firmata.IS_PIN_SERVO(pin)) {
_ pinConfig[pin] = SERVO;_
if (!servos[BLE_Firmata.PIN_TO_SERVO(pin)].attached()) {
_ }
case I2C:_
if (BLE_Firmata.IS_PIN_I2C(pin)) {
_ // mark the pin as i2c*
* // the user must call I2C_CONFIG to enable I2C for a device*
* pinConfig[pin] = I2C;
FIRMATADEBUG.print(F(“Unknown pin mode”)); // TODO: put error msgs in EEPROM*
* }
// TODO: save status to EEPROM here, if changed*
void analogWriteCallback(byte pin, int value)
* if (pin < TOTAL_PINS) {*
* switch(pinConfig[pin]) {
case SERVO:_
if (BLE_Firmata.IS_PIN_SERVO(pin))
_ pinState[pin] = value;
case PWM:_
if (BLE_Firmata.IS_PIN_PWM(pin))
analogWrite(BLE_Firmata.PIN_TO_PWM(pin), value);
//FIRMATADEBUG.print(“pwm(”); FIRMATADEBUG.print(BLE_Firmata.PIN_TO_PWM(pin)); FIRMATADEBUG.print(","); FIRMATADEBUG.print(value); FIRMATADEBUG.println(")");
_ pinState[pin] = value;
void digitalWriteCallback(byte port, int value)
//FIRMATADEBUG.print(“DWCx”); FIRMATADEBUG.print(port, HEX); FIRMATADEBUG.print(" “); FIRMATADEBUG.println(value);
byte pin, lastPin, mask=1, pinWriteMask=0;_
if (port < TOTAL_PORTS) {
_ // create a mask of the pins on this port that are writable.
lastPin = port8+8;

* if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS;*
for (pin=port8; pin < lastPin; pin++) {
// do not disturb non-digital pins (eg, Rx & Tx)_
if (BLE_Firmata.IS_PIN_DIGITAL(pin)) {
_ // only write to OUTPUT*
* // do not touch pins in PWM, ANALOG, SERVO or other modes*
* if (pinConfig[pin] == OUTPUT) {
pinWriteMask |= mask;
pinState[pin] = ((byte)value & mask) ? 1 : 0;
mask = mask << 1;
//FIRMATADEBUG.print(F(“Write digital port #”)); FIRMATADEBUG.print(port);
//FIRMATADEBUG.print(F(” = 0x")); FIRMATADEBUG.print(value, HEX);
//FIRMATADEBUG.print(F(" mask = 0x")); FIRMATADEBUG.println(pinWriteMask, HEX);_
BLE_Firmata.writePort(port, (byte)value, pinWriteMask);
_ }

PaulS…Are you saying that the Firmata code cannot send a message based on state change of pin 4?

Thanks for your response as well.

Are you saying that the Firmata code cannot send a message based on state change of pin 4?

With Firmata installed, Arduino is, usually, doing nothing but waiting for the next instruction from the master. So, no, it does not notice that the pin state has changed. It would be up to the master to poll often, to discover that the state changed, and then to do something about the change.