Week 9: Give Some Space to Your Soft Toys Too!

This week we had to use both analog and digital input and connect them to LEDs to give analog and digital input. When professor mentioned soft toys in class, my mind went back to my own soft toy, a small cuddly raccoon. I thought about how most people hug their soft toys, but maybe when they are in distress they might forget how much force they are actually applying and end up hurting their soft toy by squeezing too hard.

So, my idea was to use a force sensor and attach it to a soft toy, and using the analog input, either a green, yellow or red LED will slowly light up using mapped values, depending on the force values (which go from around 0 to a 1000). The digital input would be a button which would switch on a blue LED to indicate you are using the set-up. Once the red LED lights up and there’s an overload, the blue light will start blinking to get your attention so that you can stop holding your soft toy so tight.

This project took me a lot of time to complete, because I couldn’t debug it properly before the submission day (I know, I know, I did it again) and then I spent the whole of next day and night sleeping after taking a painkiller after my Pfizer booster shot. Major major thanks to Eric, who helped me figure out today why it wasn’t working and finally get the outcome I wanted. It only took a few lines of code to be changed in the end, as is usually the (very frustrating) case in coding.

The first problem I had was that the analog input wasn’t working as it should, even though I reinitialised each LED every time in the loop. This was fixed by making a function for the default (LOW) states and calling them in each if block. And then, once analog was working, the digital part suddenly didn’t light up as brightly as before but I realised that was because I had forgotten to declare the pinMode for the blue LED in the mess of copy and pasting. Apart from that, digital output mostly involved Frankensteining different bits of code from class.

Here is the code, and the comments will hopefully make it clear:

const int force = A0;
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 7;
const int button = 2;
unsigned long timer = 0;
int timerAmount = 100;
bool flip = false;
bool onOff = false;
bool prevButtonState = LOW;
bool overload = false;

void setup() {
  
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(button, INPUT);
  timer = millis();
}

void loop() {
  
  int mappedValue;
  int forceValue = analogRead(force);

  Serial.print("Force value: "); //for debugging
  Serial.println(forceValue);

  //green light
  if (forceValue <= 300) {
    off();
    mappedValue = map(forceValue, 0, 300, 0, 255);
    analogWrite(led3, mappedValue);
    overload = false;
  }

  //yellow light
  else if (forceValue <= 750) {
    off();
    mappedValue = map(forceValue, 301, 750, 0, 255);
    analogWrite(led2, mappedValue);
    overload = false;
  }

  //red light
  else if (forceValue <= 1100) {
    off();
    mappedValue = map(forceValue, 751, 1000, 0, 255);
    analogWrite(led1, mappedValue);
    overload = true;
  }

  //to switch on the blue light once
  bool currentState = digitalRead(button);
  if (currentState == HIGH &&  prevButtonState == LOW) {
    onOff = !onOff;
    digitalWrite(led4, onOff);
  }

  prevButtonState = currentState;

  //to make the blue light blink when there's an overload ie the red light is on
  if (overload == true) {
    if (onOff == true && millis() >= timer) {

      flip = !flip;
      digitalWrite(led4, flip);
      timer = millis() + timerAmount;

    }
    else if (currentState == LOW) {
      digitalWrite(led4, LOW);
    }
  }
}

//default state 
void off() {
  analogWrite(led1, LOW);
  analogWrite(led2, LOW);
  analogWrite(led3, LOW);
}

Here is how the circuit works:

And here are some pictures of the circuit, along with how it looked after I attached it to my raccoon using tape:

And this is a final demonstration of how it is supposed to work:

I think that’s all, I can’t think of anything else to add here except for a thank you to Eric once again and apologies for the late post (once again)!

Leave a Reply