1

I'm getting some very strange behaviour from a Haptic feedback system I'm trying to make. The test code works fine as a Multiple-In One-Out in the main loop, but once I moved the code to the Struct, I ended up with very erratic behaviour on the motor that is completely independent of pressure on the FSR and bears no correlation.

Bizarrely, the serial data that I have been using to debug the code is also largely unusable now.

Does anyone have any idea what's going on? (p.s. Don't worry about the current draw of the motor, I'm using tiny mobile phone vibration motors specifically for their low voltage and current draw)

Note: All '[' chars in the code and log have been replaced with '<' and ']' with '>' so that I can actually post this question without the site screaming at me about more than 8 links.

main.cpp:

#include <Arduino.h>
#include "sensorWeighting.h"

const int DIAG_PIN = 2;
short int numMotors;

sensorWeighting* MOTORS;

void setup() {
  Serial.begin(9600);
  MOTORS = new sensorWeighting<1>;
  //motor 1
  MOTORS<0> = sensorWeighting(9,2,new int<2>{0,1},new int<2>{100,60});
  numMotors = 1;//sizeof(MOTORS) / sizeof(sensorWeighting);
}

void loop() {
  //Serial.println("Start Loop.");
  //Serial.print("numMotors = ");
  //Serial.println(numMotors);
  for (short int index = 0; index < numMotors; index++) {
    //Serial.print("Updating Motor: ");
    //Serial.println(index);
    MOTORS<index>.update();
  }
  //Serial.println("End Loop.");
}

sensorWeighting.h:

#include <Arduino.h>
class sensorWeighting {
  private:
    short int output;
    short int inputNum;
    int* input;
    int* weight;

  public:
    sensorWeighting(){}
    sensorWeighting(short int pinOut, short int numInputs, int* inputs, int* weights){
    pinMode(pinOut, OUTPUT);
    output = pinOut;
    inputNum = numInputs;
    input = new int<inputNum>;
    weight = new int<inputNum>;
    for (short int index = 0; index < inputNum; index++) {
      pinMode(inputs<index>, INPUT);
      this->input<index> = inputs<index>;
      this->weight<index> = weights<index>;
    }
  }
    virtual ~sensorWeighting(){
    delete &output;
    delete &inputNum;
    delete<> input;
    delete<> weight;
  }

  void update(){
    Serial.print(inputNum);
    Serial.print(" inputs. ");
    int out = 0;
    for (short int index = 0; index < inputNum; index++) {
      int rawVal = analogRead(input<index>);
      Serial.print("raw input :" + index);
      Serial.println("value: "+rawVal);
      int val = (weight<index>*rawVal)/90;
      if (val>out){
        out = val;
        }
      Serial.print("Evaluating input: Input<");
      Serial.print(input<index>);
      Serial.println("] = " + analogRead(input<index>));
    };
  Serial.println("Raw Output: " + out);
  Serial.print("Trimmed Output: ");
  Serial.println(out/4);
  Serial.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
  analogWrite(output, out/4);
    };
};

typedef class sensorWeighting senorWeighting;

Circuit Diagram

Serial Log:

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :

~w[�?��x��o{Ӿ��˗��{��"����0�������^�������o?WwZ�{����[e���������������?�����/������ѧ����������������}��w����i����s���c�Mo}�����׷ʾ������Ф�m�Y�zo������ =��������������)��ۺW{r�[w����g׬����-����ߦ]��{�{�w��y�߻��(�?�^���[%}U��7�� ���|����������}W�ֶo���]��-�ݔ]�������Yo0 ��쎖w��z9���}��]o���[���K���?9^K����b�����%�s����;S+S���[}Y�1���^

Trimmed Output: 27

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :luating input: Input<

Evaluating input: Input<1> =

put:

Trimmed Output: 1

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

aw input :Ӿ��˗��{��"����0�������^�������o?WwZ�{����[e���������������?�����/������ѧ����������������}��w����i����s���c�Mo}�����׷ʾ������Ф�m�Y�zo������ =��������������)��ۺW{r�[w����g׬����-����ߦ]��{�{�w��y�߻��(�?�^���[%}U��7�� ���|����������}W�ֶo���]��-�ݔ]�������Yo0 ��쎖w��z9���}��]o���[���K���?9^K����b�����%�s����;S+S���[}Y�1���^ ޯ�3���������}�/jv����|��O讝����Ͷ�����������������Ϸ�+�l��s�}m����������������o�~�~� 8

Evaluating input: Input<1> =

Trimmed Output: 66

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2 inputs. raw input :value:

Evaluating input: Input<2278> =

aw input :value:

Evaluating input: Input<1> =

Raw Output:

Trimmed Output: 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

2
  • 1
    char array concatenation doesn't work with +. – gre_gor Dec 13 '17 at 21:28
  • @gre_gor cheers for that, I'm getting much more usable serial data now. I can see that the inputs are working, so it's an issue in the workings between I and O. – Minothor Dec 13 '17 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.