Chattering Relay

The Idea is to use a 10K potentiometer to change the LCD to display 32 different “speeds” and at the same time turn on and off 8 different outputs
The 10K pot. is wired to A0 and is working well.
The LED display is serial and when not active in a loop it too works well, when active in a loop it flickers.
The outputs are wired to a 8 Relay module that is powered by a extra 5vdc supply, it too works well as long as there is only one condition.

The Problem I am having is in the code (that I have copy and pasted a lot) is after testing the a " if ((sensorValue >= 64) && (sensorValue < 96))
{{ digitalWrite(Relay_25, LOW);}
mySerial.write(12); //Clear
delay(10);
mySerial.print(".021 in.pm");
mySerial.write(13);
mySerial.print(".52 mm.pm");}

else { digitalWrite(Relay_25, HIGH);}

by its self works great relays are solid LCD flickers a little but useable
as soon as I put more “if” conditions in the loop the relays change but chatter on and off at a fast rate

Any Ideas?

Oh yea this is my first project and first posting and don’t mean to offend anybody with my lack of knowledge or that they might see that I have used there code inappropriately.

#include <Boards.h>
#include <Firmata.h>
// These constants won’t change. They’re used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to //
const int TxPin = 6;
#define RELAY_ON 0
#define RELAY_OFF 1
/-----( Declare objects )-----/
/-----( Declare Variables )-----/
#define Relay_25 8 // Arduino Digital I/O pin number
#define Relay_26 9
#define Relay_27 10
#define Relay_31 11
#define Relay_70 12
#define Relay_33 13

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

int sensorValue = 0;
int outputValue = 0;// value read from the pot
// value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
digitalWrite(Relay_25, RELAY_OFF);
digitalWrite(Relay_26, RELAY_OFF);
digitalWrite(Relay_27, RELAY_OFF);
digitalWrite(Relay_31, RELAY_OFF);
digitalWrite(Relay_70, RELAY_OFF);
digitalWrite(Relay_33, RELAY_OFF);
pinMode(Relay_25, OUTPUT);
pinMode(Relay_26, OUTPUT);
pinMode(Relay_27, OUTPUT);
pinMode(Relay_31, OUTPUT);
pinMode(Relay_70, OUTPUT);
pinMode(Relay_33, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset
pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);
mySerial.begin(9600);
delay(100);
mySerial.write(17); //Back light on
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);

// print the results to the serial monitor:
Serial.print(“sensor = " );
Serial.print(sensorValue);
Serial.print(”\t output = ");
Serial.println(outputValue);

if ((sensorValue >= 0) && (sensorValue < 32))
mySerial.write(12); //Clear
delay(10);
mySerial.print(".012 in.pm");
mySerial.write(13);
mySerial.print(".28 mm.pm");
delay(2);
if ((sensorValue >= 32) && (sensorValue < 64))
{{ digitalWrite(Relay_26, LOW);}
mySerial.write(12); //Clear
delay(10);
mySerial.print(".016 in.pm");
mySerial.write(13);
mySerial.print(".4 mm.pm");}

else { digitalWrite(Relay_26, HIGH);}

delay(2);
if ((sensorValue >= 64) && (sensorValue < 96))
{{ digitalWrite(Relay_25, LOW);}
mySerial.write(12); //Clear
delay(10);
mySerial.print(".021 in.pm");
mySerial.write(13);
mySerial.print(".52 mm.pm");}

else { digitalWrite(Relay_25, HIGH);}

delay(2);

if ((sensorValue >= 96) && (sensorValue < 128))
{{ digitalWrite(Relay_25, LOW);}
digitalWrite(Relay_26, LOW);
mySerial.write(12); //Clear
delay(10);
mySerial.print(".029 in.pm");
mySerial.write(13);
mySerial.print(".71 mm.pm");}
else { digitalWrite(Relay_25, HIGH);
digitalWrite(Relay_26, HIGH);}

delay(2);

}

analog_milling_speed_feed.ino (2.86 KB)

Dry run your code with sensorValue = 100 and look at relay 26 and you’ll see why.

The structure is all wrong - you need to work out what condition is needed for each relay independently, not try to sequentially test various input values that each affect various relays.

So more like

  if ((sensorValue >=32 && sensorValue < 64) || (sensorValue >= 96 && sensorValue < 128))
  {
    digitalWrite (Relay_26, LOW) ;
    ....
  }
  else
    digitalWrite (Relay_26, HIGH) ;

So long as each if-clause controls one relay each they cannot interfere like they are doing now.

OK I made a graph of what I am trying to accomplish this is to control a milling machine that has 32 speeds controlled by 6 relays. My goal is to use the 10K pot to select the speed via the LCD display. I am more than happy to use your suggestion but I might run into a different problem, I am open to any ideas hopefully this will help

sensor value Relay_output 25 26 27 31 70 33 0-32 0 0 0 0 0 0 32-64 0 1 0 0 0 0 64-96 1 1 0 0 0 0 96-128 0 0 1 0 0 0 128-160 0 1 1 0 0 0 160-192 1 0 1 0 0 0 192-224 1 1 1 0 0 0 224-256 0 0 0 0 1 0 256-288 0 1 0 0 1 0 288-320 1 0 0 0 1 0 320-352 1 1 0 0 1 0 352-384 0 0 1 0 1 1 384-416 0 1 1 0 1 1 416-448 1 0 1 0 1 1 448-480 1 1 1 0 1 1 480-512 0 0 0 1 0 0 512-544 0 1 0 1 0 0 544-576 1 0 0 1 0 0 576-608 1 1 0 1 0 0 608-640 0 0 1 1 0 0 640-672 0 1 1 1 0 0 672-704 1 0 1 1 0 0 704-736 1 1 1 1 0 0 736-768 0 0 0 1 1 0 768-800 0 1 0 1 0 0 800-832 1 0 0 1 1 0 832-864 1 1 0 1 1 1 864-896 0 0 1 1 1 1 896-928 0 1 1 1 1 1 928-960 1 0 1 1 1 1 960-992 1 1 1 1 1 1 992-1024 [/table]

The Table is off but I think you'll get the idea

Thanks

so u want to map the range 0..1023 to y=0..63?

this forumla should do that: y = ((x+1023/(63*2))*63)/1023

then the relay states r as follows: r25=y&1; r26=y&2; r27=y&4; r31=y&8; r70=y&16; r33=y&32;

Beware....

(I'm writing as if your machine only needed four bits of input, just to shorten this. Ideas extend to multiple bits.)

If you want to send, say, 1100 to the machine, and at the moment, the relays are sending it 0000, you are unlikely to be able to go directly to 1100. Even if it is only briefly, the relays will for a moment either be 0100 or 1000 before they change to the 1100 you want.

This may or may not be important. But at least consider it.

The machine may have been designed to respond to a...

http://en.wikipedia.org/wiki/Grey_code

... in which case, as long as you work thought the "gears", the "problem" goes away.

Follow the advice by RIDDICK if you have a nice division of values. However, if your table and relays are arbitrary:-

You need two arrays to do this. One has the upper limit of each sensor position. The other has a number made from the bit pattern of the on off state of each relay for each band.

Then take your reading and use a for loop to go throught the sensor value array. If the reading is lower than the array value store the loop index in a variable. After the loop that variable will contain the index into the array of numbers that define your relay states. Then use that to open or close your relay. You should be able to do this in about ten lines.

If you are not sure about arrays see:- http://www.thebox.myzen.co.uk/Tutorial/Arrays.html