Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: send serial ONLY when sensors are touched? on: March 03, 2013, 11:12:46 am
Thanks everyone, some valuable suggestions;


I'm attracted to this solution the most, could you perhaps pseudo-code this for me? Is this close?:

for (loop to read all pins)

which pin >0 ??
send a pair: pin# and value

2  Using Arduino / Programming Questions / send serial ONLY when sensors are touched? on: March 03, 2013, 05:25:10 am
Hallo all

I am using the following code to detect force from multiple FSRs, and return that value as brightness, for multiple LEDs. That bit is easy. I am also sending this data to MaxMSP, which uses a "metronome" type object to poll the serial port.

//read 12 FSRs, convert force value to LED brightness

int inVal;
int brightness;

void setup(){
  //multiple LED pins/////////
  for(int x=2; x<14; x++){
    pinMode(x, OUTPUT);

void loop(){
  //multiple FSR pins
  for(int i=0; i<12; i++){
    inVal = analogRead (i);
    inVal=constrain(inVal, 0, 255);
    //prepend sensor value with pin# ident
    analogWrite((i+2), brightness);

My problem is the fact that the Arduino code is continually running, sending sensor ID and value - for example, at any time it could be sending "5, 0; 6, 0; 7, 143; 8, 0; when I begin polling the serial port I might get EITHER the sensor value first followed by sensor ident, OR ident then value. I thought that a solution might be to only send the data to MaxMSP when a sensor is pressed. How would I do this?

if sensorVal >0{
send all the data;
blank space here;

Would this result in jittery data? Or is another solution to listen to the serial port and wait for a keystroke or ascii value (from MaxMSP). I know how to send stuff from Arduino to Max, but have no idea how to do this the other way round. What I wish to achieve is a guarantee that whenever I begin polling the serial port I will always get sensor ident FIRST, followed by the associated sensor value.

Any suggestions?
3  Using Arduino / Programming Questions / Re: send serial selectively (only when active)? on: November 27, 2012, 09:41:52 am
yep, you beat me to it:

int currVal;
int prevVal;
int delta;

void loop(){

thanks for all your help

4  Using Arduino / Programming Questions / Re: send serial selectively (only when active)? on: November 27, 2012, 09:02:22 am
Yes, I tried my own example and my 'sent' data is unreliable. As my FSRs are homemade, the raw values fluctuate from c.120 (+/-5) to c. 800 (+/-5) so I was using [map] and [constrain] quite heavily to force the inactive value down to zero. Could I ask for further help in applying a delta function; this way I can send data only when the delta exceeds a set threshold.

thank you
5  Using Arduino / Programming Questions / send serial selectively (only when active)? on: November 27, 2012, 07:27:08 am

I am using serial.write to send data from 12 diy FSR's - to MaxMSP - and I recently learned from a more experienced colleague that it is common practice to send data only when it changes. This sounds attractive. Would it simply be a matter of writing the following:

int var=analogRead(myPin);

if (var > 0){
Serial.write (var);
//do nothing

Will this 'mute' or clear the serial buffer when my sensors are inactive? I guess my question is, how do I send my data only when the sensors are sending values > 0?

Apologies for poor wording;
and thanks
6  Using Arduino / Programming Questions / Re: variable resistor + edge detection? on: July 12, 2012, 11:36:09 am

I tried rather inexpertly to hack together the idea above with my existing code; due to my limited skillz I couldn't get it to roll.

I did however get the functionality I wanted (turn a momentary switch into a latching one and toggle between two LEDs), without using a timer:

Latching functionality from a momentary switch - toggles between two LEDs;
this sketch combines elements of the examples StateChangeDetection and Debounce;
it reads a switch on pin2, counting the number of HIGH readings (%2).
Brendan McCloskey 2012
int counter=0;     //count # of presses
int newState=0;    //is switch open or closed
int lastState=0;   //what was previous state

void setup(){
  pinMode(2, INPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);

void loop(){
  //local var initialized every loop; reads input pin
  int readPin=digitalRead(2);
  //if state changes AND pin is HIGH...
  if (readPin != lastState){
    if (readPin == HIGH){ 
    //...accept as true reading
  //binary 'function'
  if (counter==0){
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
  //update previous state

Thanks for all the helpful input

7  Using Arduino / Programming Questions / Re: variable resistor + edge detection? on: July 10, 2012, 01:57:08 pm
Hallo gents, and thanks for the (once again) prompt replies.

Whilst working on this today, I thought it best to stick to testing the code with a switch - I can implement the FSR later. Apologies for not being 100% clear on this; what I wish to do is (via code) turn a momentary switch into a latching one, such that an LED lights upon the first press of the switch (switch 0 to 1 transition, or FSR > x), and then the LED is off when the next press occurs (switch 0 to 1, or FSR > x).

I will try to implement the 'done' variable in my current hack, but what is (clearly) apparent is that when the contact is made the digitalRead function is continuously returning a stream of 1's, where I only want one 'hit'.

The code below is a hack of the buttonStateDetection and Debounce examples; it doesn't work as I wish - it flashes the LED when button is pressed, what I want the code to do is toggle the state of the LED:
//use millis() as a timer to check if state change is > wait time
//combines debounce and statechange examples

int newState = 0;//current state, on or off
int oldState = 0;//previous state, off or on
int switchPin = 2;
int ledPin = 12;

long newTime = 0;//time at which state changed
int noiseDelay = 50;//wait time; change to test
int pressCount = 0;

void setup(){
  pinMode(switchPin, INPUT);//switch connection
  pinMode(ledPin, OUTPUT);//LED+ (anode, long)

void loop(){
  int readState = digitalRead(switchPin);//read the switch's state and store it
  if (readState != oldState){//if state has changed
    newTime = millis();
  if ((millis() - newTime) > noiseDelay){//if wait time has passed
    newState = readState;//true reading
    if (newState==HIGH){//if off --> on
      pressCount = 0;
    pressCount = pressCount%2;
    digitalWrite(ledPin, pressCount);//assign reading to output
  oldState = readState;

thanks again Team-Arduino
8  Using Arduino / Programming Questions / variable resistor + edge detection? on: July 10, 2012, 05:18:06 am
I am currently examining the ButtonStateChange example sketch, to hack/adapt it to suit my needs; using an FSR I wish to detect when the current changes from 0 to >0 - using the FSR as a momentary switch (conventional switch design/shape/housing doesn't suit my needs), such that I can get ONE event from the state change. Is there a simple function out there that does edge detection like this?

if (sensorVariable>0){
do this once only

9  Using Arduino / Programming Questions / Re: serial.available question on: June 04, 2012, 09:41:21 am

again I was incorrect: SARCduino doesnot use a handshake, it uses an interrupt driven timer
10  Using Arduino / Programming Questions / Re: serial.available question on: June 04, 2012, 03:54:05 am
chastised and contrite

The problem, in complete layman's terms, is that it appears that the stream of bytes going out via Serial.write (to Max) is creating a log-jam and causing my other program (Max) to hang, until I reinitialize serial communication (in Max). Maybe simply increasing the delay(20); to something more leisurely would help.

I have seen examples of serial communication 'prefab' sketches such as Maxuino and SARCduino that appear to use a call/response function. This is voodoo to me, or at least, not transparent, so can I ask for someone to explain that function. I could easily post the above 'prefab' example and ask for someone to decipher it but that would appear lazy. I'm not posting on the Max forum as this is specifically a sketch question.

Y'know, maybe just increasing the delay() value will solve this
11  Using Arduino / Programming Questions / Re: serial.available question on: June 04, 2012, 03:20:23 am
Many apologies for my confused and  incorrect use of terminology (and earlier impatience);

Arduino uses 10-bit ADC on the incoming analog values; Serial.write outputs in 8-bit bytes


And I'll use the Serial call and response method
to troubleshoot my MaxMSP hanging problem

//DIY fsr; 1kR = c.0-800
//DIY FSR value translates to LED brightness
//8 consumer FSRs on A0-A7
int padLED;//DIY FSR
int matrix;//consumer FSRs
int brightness=0;//out pin 13
int weight=40;//threshold weight of rubberPad on FSR
void setup(){

void loop(){
  //cycle-read 8 inputs, 0-7
  for(int x=0; x<8; x++){
  //header+value pair sent to Max
  //Serial.write("  ");//debugging only
  //Serial.write("  ");//ditto
  //read padLED
  padLED=analogRead(A15);//DIY FSR
  padLED=map(padLED, weight, 200, 0, 255);
  padLED=constrain(padLED, 0, 254);
  brightness=pow(brightness, 1.45);
  brightness=constrain(brightness, 0, 255);
  analogWrite(13, brightness);
  //unique header+DIY FSR value pair, sent to Max
  //Serial.println(brightness);//debuggin only

12  Using Arduino / Programming Questions / Re: serial.available question on: June 03, 2012, 03:15:18 pm
Thank you again majenko for the clarity

my original misconception stems from my "10-bit ADC - 8-bit serial" confusion; if I send my analog sensor data to Max >255 it folds over. I stupidly assumed that all Arduino users are familiar with the Max [serial] object - totally my bad, for which I apologize; please pass this apology on to the "terminology police".

I have sourced a solution elsewhere, with your help majenko

13  Using Arduino / Programming Questions / Re: serial.available question on: June 03, 2012, 02:54:13 pm
Hopefully, given my clearly-stated n00b status, may I clarify my original question and get back to the clearly-stated core problem:

Arduino converts analog input to digital output via 10-bit munch/thing/worm/smeg/artefact (please start another related thread on arcane terminology)

This is clearly irrelevant; can any non-pedant help me write a call/catch or call/response or whatever the correct f*cking arcane terminology is, "function" that reads all the analog inputs, fills a properly-sized buffer and then sends the bundle out via Serial.write such that the Max [serial] object doesn't freak out

and peace/love to all
14  Using Arduino / Programming Questions / Re: serial.available question on: June 03, 2012, 01:34:16 pm
criticism without advice
total n00b, sorry

very appropriate forum name "grumpy"; arduino reference clearly states that serial data is converted to a 10-bit length byte

thank you majenko for clarifying this for me; I can now go and research a call/response function
15  Using Arduino / Programming Questions / serial.available question on: June 03, 2012, 12:29:08 pm
I am using a mega2560 to send sensor data (16 force sensors in parallel) to MaxMSP; the serial.write command allows me to send 10-bit bytes to the [serial] object in Max. Recently, my patches have started to hang, leading me to reassess my sketch, and I've noticed that other such sketches tend to feature the Serial.available command, which I've never used before. The reference page says that this returns the number of incoming bytes, and it appears to be used to flush the serial buffer (total n00b, sorry). Can I simply add this command like this:
if (serial.available>0){
do all the reading here

Will this apparently simple approach ensure that I don't overfill the serial buffer and thereby prevent Max from hanging? In a recent conversation with a colleague, he mentioned that he avoided using the [serial] object in Max as it was unreliable for this very reason,(buffer overfill) and preferred to use Processing,

Pages: [1] 2 3