Pages: [1]   Go Down
Author Topic: Pressure sensor - Changing colours of RGB LED  (Read 1912 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has anyone got any good tutorials for a pressure sensor? The harder you press it would change the colour?

Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what kind of pressure do you want to read?
What is the range?
What is the #samples per second/hour?

What do you consider a good tutorial? I don't know your background so please tell if you are familiar with SW (what) hW (what) etc.

with respect to the colors, this might be ina interesting read - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207331496 -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think a good tutorial would be showing where the pins, wires etc goes on the breadboard.
You will like this one - http://www.earthshineelectronics.com/files/ASKManualRev5.pdf - I think
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The previous link to the pdf is 402 (Payment required). In any case, I'm wondering if this thread was ever resolved as I am now looking for some reference to what code elements I will need. I'm looking to also use  this FSR http://www.adafruit.com/products/1075?gclid=CMS_7rKQ4r4CFZFufgodPywAzg in combination with an RGB LED (honestly not sure if common cathode or anode, but it's configured as it is here https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/breadboard-layout.

Then I'm using  this code to get a white light (combined RGB 0-255 values) mapped to 0-1023 analog FSR values:

Code:
int fsrAnalogPin = 0; // FSR is connected to analog 0

int LEDpin = 11;      // connect ___ LED to pin 11 (PWM pin)
int LEDpin2 = 10;      // connect ___ LED to pin 10 (PWM pin)
int LEDpin3 = 9;      // connect ___ LED to pin 9 (PWM pin)

int fsrReading;      // the analog reading from the FSR resistor divider

int LEDbrightness, LEDbrightness2, LEDbrightness3;

void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  pinMode(LEDpin, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
  pinMode(LEDpin3, OUTPUT);
}

void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  // we'll need to change the range from the analog reading (0-1023) down to the range
  // use analogWrite (0-255) with map!
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  LEDbrightness2 = map(fsrReading, 0, 1023, 0, 255);
  LEDbrightness3 = map(fsrReading, 0, 1023, 0, 255);
  
  // LED gets brighter the harder you press
  analogWrite(LEDpin, LEDbrightness);
  analogWrite(LEDpin2, LEDbrightness2);
  analogWrite(LEDpin3, LEDbrightness3);
 
  delay(100);
}

The  code above is what I have so far -  building from a previous sketch that controls LED brightness through FSR analog reading values using the map function. I have it creating white light (R+G+B values all mapped to one FSR analog reading), however now I want to set a range of analog values to control separately the R, G, and B values from the RGB LED. I'm pretty sure I just need to play around with some if/else statements and this is where I am at now. I'm not to familiar with the coding languages other than some HTML and CSS, so I'm learning how to utilize this syntax so it makes sense. I'll continue fumbling around with the code and asking other friends in the meantime.

Also, this is my first time posting so if I've neglected some forum etiquette please let me know and thank you for your time! I'm not looking for someone to do my coding for me, but ideally I'm looking for some validation that I'm at least looking in the right direction and if someone has the time and energy to offer, I'm looking for an explanation on where to put my if/else statement, how to structure it and why it is structured in such a way. I'll be happy with any help (and no help for that matter, I'm sure I'll get it sorted out eventually).


« Last Edit: June 07, 2014, 09:06:45 pm by cdastan » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

is in practice

 LEDbrightness =  fsrReading/4;
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

is in practice

 LEDbrightness =  fsrReading/4;

I'm sorry, this is unclear to me. I'm aware that

 LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

is what is currently mapping one of my colors (red at the moment).

What do you mean to suggest with

LEDbrightness =  fsrReading/4;

??? I don't know why I should divide that value by 4 as opposed to mapping it and creating a series of constraints for my analog values to coordinate with my R, G, and B values. I'm trying to create a custom listing of colors within differing ranges of analog readings.

How can I best create a listing of analog values to rgb values? I want to experiment with color mixing based on analog value inputs to be my first form of feedback in affective computing tests on haptics and color with emotion.

I am wondering if I should be using the switch/case here, however I'm not quite sure how to utilize it properly in this context. Let me know if you think I should be using switch/case or the constrain codes for this application.

Thank you again for your time.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

maps the fsrReading value  0 -> 0  and  1023 -> 255  and assigns this value to LED brightness?

LED =  map(fsrReading, 0, 1023, 0, 255) 
== fsreading * (255 -0) / (1023 -0) 
== fsreading * 255 /1023
== fsreading / (1023/255)
== ~~ fsreading /4;

makes more sense now?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So instead of mapping, you're suggesting I divide my analog sensor values by 4 to map them to a 0-255 value?

It makes a little more sense, but I am very new to the coding language so it's still quite confusing unless you notate what each piece of code iw for and where it needs to be within the sketch. I'm learning and I apologize if it's frustrating if I'm not getting this right away.

I'm not quite catching on how I can use the value from fsrReading/4; to set ranges of analog readings to a custom set of color mixing parameters.

An example for color mixing logic
Analog values from 20-500 should control red
Analog values from 300-800 should control green
Analog values from 600-1000 should control blue

I want them to overlap so the colors mix, does that make sense?

Please let me know if I'm not being clear in what I am trying to do or if I should be familiarizing myself with something before I start asking these questions. I'm still looking through the reference material and trying my best to practice and learn the code, but currently I'm stumped. If you have the patience, I greatly appreciate continued guidance.
« Last Edit: June 08, 2014, 02:21:22 pm by cdastan » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So instead of mapping, you're suggesting I divide my analog sensor values by 4 to map them to a 0-255 value?
Yes, for that specific mapping the formulas are functional equal in practice.


Quote
An example for color mixing logic
Analog values from 20-500 should control red
Analog values from 300-800 should control green
Analog values from 600-1000 should control blue

This needs a bit more complex mapping, especially the green one...
Below the translation into code of your example behaviour.

Code: (not tested, not complete)
void loop()
{
  // MEASUREMENT
  int raw = analogRead(A0);


  // DO THE MATH
  // 20 is most red;     500 is no red anymore
  int red = map(raw, 20, 500, 255,  0);    // note the mapping is 'reverse'
  red = constrain(red 0, 255);  // keep the values within limits
 
  // green is max at 550 and less in both directions
  int green =  255 - abs(raw - 550);    //  see below (1)
  green  = constrain(green,  0, 255);   // keep the values within limits

  // blue increases from 600 -- max.
  int blue = map(raw, 600, 1023, 0, 255);
  blue = constrain(blue, 0, 255);   // keep the values within limits


  // DISPLAY CALCULATED VALUES
  analogWrite(REDLED, red);
  analogWrite(GREENLED, green);
  analogWrite(BLUELED, blue);
}

(1) the function abs(x) returns  x if x > 0  and return  -x otherwise. So it returns always a positive number.
abs(raw-550)   is the 'distance between raw and the value 550'

(2) note that the map function is a linear interpolation.
If the input value is outside the input range, the output will be outside the output range.
that is why constrain() is used.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob,

Thanks! The code works and I've learned a great deal. I appreciate the help, it's a great first experience posting in these forums.

-Chris
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to hear it all works!
Keep on learning, and always take the time to ask yourself the question:

"if I were the computer, what are the steps i need to do to solve my project"

and refine those steps.

on high level it boils often down to
1) make measurements (sensor, serial input whatever)
2) do math
3) display results (or move motor / servo)

then refine those steps.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: