Hacking Sweet Feedback

In each Sweet Feedback is an Arduino microcontroller that can be programmed to do specific things. It's programmed using the Arduino processing language. Below are some examples of programs we have made in this language for Sweet Feedback.

Simple Arduino Sensor Debugging Code

 

// These constants won't change. They're used to give names

// to the pins used:

const String MachineID = "0131";

// Sensors' pin

const int MicInPin = A0; // Analog input pin that the microphone is attached to

const int PhotodlnInPin = A1; // Analog input pin that the photodarlington is attached to

const int ThermtrInPin = A2; // Analog input pin that the thermistor is attached to

//Actuators' pin

const int MotorOutPin = 13; // Motor connected from digital pin 13 to ground

const int SpeakerOutPin = 8; // Speaker connected from digital pin 8 to ground

const int sensorNum = 3;

int sensorValue[sensorNum] = {0}; // initialize value read from the pot

int outputValue[sensorNum] = {0}; // initialize value output to the PWM (analog out)

int print_mask[sensorNum] = {0}; // 0 if the data is not going to show, 1 otherwise

const int numberOfSamples = 16;

int sample;

long signal[numberOfSamples];

long runningAverage;

long sumOfSamples = 0;

int counter = 0;

int sample2;

long signal2[numberOfSamples];

long runningAverage2;

long sumOfSamples2 = 0;

int counter2 = 0;

int balanceMicVal = -1;

enum tempUnit{Kelvin,Celcius,Fahrenheit};

const int numReadings = 500;

int micReadings[numReadings] = {0};

long total = 0;

int index = 0;float outputNoiseLevel = -1;

//for group collaboration experiment

int count = 0;

void setup() {

  // initialize serial communications at 9600 bps:

  Serial.begin(9600);

  pinMode(MotorOutPin, OUTPUT);

  digitalWrite(MotorOutPin, LOW);

  for (int i = 0; i <= numberOfSamples; i++) {

    sample = analogRead(MicInPin);

    signal[i] = abs (sample - 512);

    sumOfSamples = sumOfSamples + signal[i];

  }

  establishContact(); // send a byte to establish contact until receiver responds

}

void loop() {

  getSensorData();

  serialCallResponse();

  delay(50);

}

// Main Loop Tasks

void getSensorData() {

  long mic_avg = 0;

  static int maxMA = 0;

  static int minMA = 1024;

  static int minMA2 = 1024;

  static int minMA3 = 1024;

  static int MADif = 0;

  static int k = 0;

  static int ma[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  int j;

  static int i = 0;

  // read the analog in value:

  sensorValue[0] = analogRead(MicInPin);

  sensorValue[1] = analogRead(PhotodlnInPin);

  sensorValue[2] = analogRead(ThermtrInPin);

  ma[i] = abs(sensorValue[0] - 145); // subtract bias

  i++;

  if (i > 9) i = 0;

  for (j = 0; j < 10; j++) {

    mic_avg += ma[j];

  }

  mic_avg = mic_avg / 10;

  if(k > 99) k = 0;

  if(k == 0)

  {

    maxMA = 0;

    minMA = 1024;

    minMA2 = 1024;

  }

  if(mic_avg > maxMA) maxMA = mic_avg;

  if(mic_avg < minMA) minMA = mic_avg;

  if(mic_avg > minMA && (mic_avg < minMA2 || minMA2 == minMA)) minMA2 = mic_avg;

  if(minMA2 == 1024) minMA2 = minMA;

  MADif = maxMA - minMA2;

  k++;

  // maps raw values

  outputValue[2] = thermistorCalibration(sensorValue[2], Celcius);

  outputValue[1] = map(sensorValue[1], 0, 1023, 0, 255);

  outputValue[0] = (MADif);

}

void serialCallResponse(){

  if(Serial.available() > 0) {

    int inByte = Serial.read();

    if(inByte != 'A' && inByte != 'B' && inByte != 'C' && inByte != 'D'

      && inByte != 'E') {inByte = 'B';}

    int i;

    if (inByte == 'A') {

      giveCandies();

    }else if (inByte == 'B') {

      for(i = 0; i < sensorNum -1; i++){

        Serial.print(outputValue[i]);

        Serial.print(",");

      }

      Serial.println(outputValue[i]);

    }else if (inByte == 'C'){

      playDisappointedSound();

    }else if (inByte == 'D'){ //incremental positive feedback

      count += 1;

      if(count >= 10){

        for (int i = 0; i <= 80; i++) {

          tone(SpeakerOutPin, (400+count*40) * pow(1+i/60.0, 4));

          delay(5);

          noTone(SpeakerOutPin);

        }

        count = 0;

      }else{

        for (int i = 0; i <= 30; i+=1) {

          tone(SpeakerOutPin, (400+count*40) * pow(1+i/60.0, 2));

          delay(7);

          noTone(SpeakerOutPin);

        }

      }

    } else if (inByte == 'E') {

      Serial.println(MachineID);

    }

  }

}

// This is the calibration function for thermistor P/N:NTCLE413E2103H400

// parameter RawADC is the analogReading from the thermistor

// parameter Unit is the temperature unit of the return value

double thermistorCalibration(int RawADC, int Unit) {

  long double temp;

  long double A,B,C,D;

// this is the coefficient for the thermistor P/N:NTCLE413E2103H400

  A = 0.0012;

  B = 2.2614e-004;

  C = 7.0822e-007;

  D = 6.7885e-008;

  double R = 1000;

  double RT = (1024*R/RawADC) - R;

// Steinhart–Hart equation

// {1 \over T} = A + B \ln(R) + C (\ln(R))^3 \,

  temp = log(RT);

  long double divisor = (A + (B + (C + D * temp)* temp )* temp);

  temp = 1/ divisor;

  temp += 4;

  if(Unit == Kelvin)

    return temp;

  else if(Unit == Celcius)

    return temp = temp - 273.15; // Convert Kelvin to Celcius

  else if(Unit == Fahrenheit)

    return temp = (temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit

}

void giveCandies(){

  digitalWrite(MotorOutPin, HIGH); // candy coming

  delay(500);

  digitalWrite(MotorOutPin, LOW);

}

void playDisappointedSound() {

  for (int i = 0; i <= 60; i++) {

    tone(SpeakerOutPin, 880 * pow(2, -i / 60.0));

    delay(5);

    noTone(SpeakerOutPin);

  }

}

void establishContact() {

  while (Serial.available() <= 0) {

    Serial.print(MachineID);

    Serial.print(",");

    for(i = 1; i < sensorNum -1; ++i){ Serial.print("0,");}

    Serial.println("0");

    delay(500);*/

    int i;

    for(i = 0; i < sensorNum -1; i++){

      Serial.print(outputValue[i]);

      Serial.print(",");

    }

    Serial.println(outputValue[sensorNum]);

    delay(500);

  }

}

 

If you want to program on a non-arduino platform or make your Sweet Feedback machine easily accessible on the web, simply use this piece of software to easily command your machine from anywhere.

 

Arduino Connection Software (For Macs, But Instructions For Everyone)

 

This file contains software that will let you easily connect to your machine. Once you download it, open the how-to word doc which will show you the setup process.

Connect!

 

For PC and Linux users, the how-to document in the file above will help you connect with your Sweet Feedback machines as well. We are currently developing software to make your connection process easier and will publish it as soon as possible.