AutoArduino AutoArduino Bluetooth doesn't send pin state

Discussion in 'Guides / Examples / Ideas Forum' started by Khalanar, Feb 14, 2017.

  1. Khalanar

    Khalanar New Member

    Joined:
    Jan 10, 2017
    Messages:
    3
    Likes Received:
    0
    Hi, I'm working on a small project. I deleted parts of the Autoarduino code that handled ethernet since I'm using Bluetooth only. Unfortunatelly something is not working right. Any help would be greatly appreciated:


    int menuItemNumber = 1;
    //Library to easily connect stepper motors
    #include <Stepper.h>
    #include <U8glib.h>

    // Henry's Bench Hello World - for use with Monochrome OLEDs


    U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);

    //#include <SPI.h>


    //Variables that help control pins 2 and 3 states so that they can be sent to AutoArduino as events
    const byte interruptPinsNumber = 2;
    const byte interruptPins[] = {2, 3};
    volatile bool shouldReads[] = {false, false};
    unsigned long lastChanges[] = {0l, 0l};
    byte lastValues[] = {HIGH, HIGH};
    byte lastPinMode[] = {INPUT_PULLUP, INPUT_PULLUP, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT, OUTPUT};

    //Runs when pin 2 changes
    void pin2Changed() {
    shouldReads[0] = true;
    }
    //Runs when pin 3 changes
    void pin3Changed() {
    shouldReads[1] = true;
    }

    bool button7CanChange = true;

    void setup() {

    u8g.setFont(u8g_font_unifont);
    u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
    Serial.begin(9600);
    pinMode (13, OUTPUT);
    pinMode (12, OUTPUT);
    pinMode (11, OUTPUT);
    pinMode (10, OUTPUT);
    pinMode (9, OUTPUT);
    pinMode (8, OUTPUT);
    pinMode (7, INPUT);
    pinMode (6, OUTPUT);
    pinMode (5, OUTPUT);
    pinMode (4, OUTPUT);
    // Pin 2 e 3 are configured for input so that AutoArduino can be alerted when they change
    pinMode (3, INPUT_PULLUP);
    pinMode (2, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(2), pin2Changed, CHANGE);
    attachInterrupt(digitalPinToInterrupt(3), pin3Changed, CHANGE);

    }


    bool connectedIpAddressSet = false;
    byte connectedIpAddress[] = {0, 0, 0, 0};
    void loop() {
    handleSerial();
    // handleEthernet();
    // For all pins that generate events (2 e 3) check if value changed

    for (int i = 0; i < interruptPinsNumber; i++) {
    byte currentValue = hasChanged(i);
    //se o valor mudo, enviar novo valor por Serial
    if (currentValue != NULL) {

    int pin = i + 2;
    int value = currentValue - 1;
    handleChangeSerial(pin, value);
    // handleChangeEthernet(pin, value);



    }
    }

    customLoop();
    }

    void handleChangeSerial(int pin, int value) {
    Serial.print("i");
    Serial.print(pin);
    Serial.print(",");
    Serial.print(value);
    Serial.print(";");
    }

    void handleSerial() {
    while (Serial.available() > 0) {
    //get mode
    char mode = Serial.read();
    //check how many values this mode expects
    int modeValues = getModeValues(mode);
    int values[modeValues];
    if (modeValues != NULL) {
    //assign all values
    for (int i = 0; i < modeValues; i++) {
    values = Serial.parseInt();
    }
    }
    //read command delimiter
    char endChar = Serial.read();
    //run command
    handleCommand(mode, values);
    }
    }

    //returns the number of values each mode expects
    int getModeValues(char mode) {
    //digital and analog write is 2
    if (mode == 'd' || mode == 'a') {
    return 2;
    } else
    //digital and analog read is 1
    if (mode == 'r' || mode == 'e') {
    return 1;
    } else
    //motor is 7
    if (mode == 'm') {
    return 7;
    }
    return NULL;
    }
    void handleCommand(char mode, int values[]) {
    if (mode == 'd') {
    handleDigitalWrite(values);
    } else if (mode == 'a') {
    handleAnalogWrite(values);
    } else if (mode == 'r') {
    handleReadDigital(values);
    } else if (mode == 'e') {
    handleReadAnalog(values);
    } else if (mode == 'm') {
    }
    }
    void handleDigitalWrite(int values[]) {
    int pin = values[0];
    int on = values[1];
    changePinMode (pin, OUTPUT);
    //Serial.println("digital");
    /*Serial.println(pin);
    Serial.println(on);*/
    //se for 1, mete HIGH, senão mete LOW
    if (on == 1) {
    digitalWrite(pin, HIGH);
    } else {
    digitalWrite(pin, LOW);
    }
    }
    void handleAnalogWrite(int values[]) {
    int pin = values[0];
    int value = values[1];
    changePinMode (pin, OUTPUT);
    //Serial.print("analog");
    analogWrite (pin, value);
    }
    void handleReadDigital(int values[]) {
    int pin = values[0];
    changePinMode(pin, INPUT_PULLUP);
    }

    void handleReadAnalog(int values[]) {
    int pin = values[0];
    changePinMode(pin, INPUT_PULLUP);
    }

    void changePinMode(int pin, byte mode) {
    if (lastPinMode[pin - 2] != mode) {
    //Serial.print("Changing mode");
    pinMode (pin, mode);
    //Delay exists because when you change from OUTPUT to INPUT and you read its value right away you get an old value. With the delay the read value is correct
    delay(10);
    lastPinMode[pin - 2] = mode;
    }
    }

    //Determins if an input pin has changed
    byte hasChanged(int i) {
    bool shouldRead = shouldReads;
    if (shouldRead) {
    unsigned long currentTime = millis();
    if (currentTime - lastChanges > 16) {
    shouldReads = false;
    lastChanges = currentTime;
    int currentValue = digitalRead(interruptPins);
    if (currentValue != lastValues) {
    lastValues = currentValue;
    return currentValue + 1;
    }
    }
    }
    return NULL;
    }
    //INSERT YOUR CUSTOM CODE HERE
    void customLoop() {
    if (digitalRead(7) == HIGH) {
    if (button7CanChange) {
    menuItemNumber++;
    if (menuItemNumber >= 5) {
    menuItemNumber = 1;
    }
    button7CanChange = false;
    }

    } else {
    //delay(10);
    button7CanChange = true;
    }
    Serial.println(button7CanChange);
    u8g.firstPage();
    do {
    draw();
    } while ( u8g.nextPage() );
    }

    void draw() {
    u8g.drawFrame(0, 0, 128, 64);
    u8g.drawFrame(3, 3, 9, 9);
    u8g.drawFrame(15, (15 * menuItemNumber) - 7, 4, 4);
    if (button7CanChange) {
    u8g.drawStr( 100, 30, "1");
    } else {
    u8g.drawStr( 100, 30, "0");
    }
    u8g.drawStr( 20, 15, "AAAAAAA");
    u8g.drawStr( 20, 30, "BBBBBBB");
    u8g.drawStr( 20, 45, "CCCCCCC");
    u8g.drawStr( 20, 60, "DDDDDDD");
    }

    Autoarduino manages to connect to the 05 module but it gets stuck when trying to read the state of pin3.

    Thanks for your time!
     
  2. Khalanar

    Khalanar New Member

    Joined:
    Jan 10, 2017
    Messages:
    3
    Likes Received:
    0
    Fixed it! I realised the script needs to read for the values in Serial!
     
  3. Poloace

    Poloace New Member

    Joined:
    Feb 7, 2017
    Messages:
    11
    Likes Received:
    1
    Could you post your updated code, I am struggling to get it to work in OTG mode, maybe I will try bluetooth
     

Share This Page