Go Down

Topic: Logic Controller - Convert Pulsing Input To PWM Output - Help (Read 1 time) previous topic - next topic

The_Doc

Sep 17, 2013, 06:39 pm Last Edit: Sep 17, 2013, 06:41 pm by The_Doc Reason: 1
Good day to you all.

I have a problem I'd like some help with, and I'm hoping you guys can be of assistance.

Firstly, please be patient with me, as I'm very new to any sort of programming and the learning curve is proving to be more of a mountain at the moment.

I have a machine which has a safety warning system. The warning system comprises of a single, two colour (red/green) LED. When the sensors detect a problem, the LED flashes orange, (red/green flashing simultaneously), and when the problem becomes critical, the status LED flashes red. The LED flashes at a frequency of approximately once per second, with a duty cycle of approximately 50% and the flashing is controlled via a control board, which sends an intermittent +3.3V signal to the LED.

I'm attempting to use an Arduino as a logic controller, to convert the signals to the LED to a PWM output to control the position of a servo motor... i.e, if the green and red (orange) are flashing at the same time, or neither are flashing, then send a PWM signal to position the servo at position 'A' and if the red is flashing alone, then send a PWM signal to position the servo at position 'B'.

I'm finally getting my head around controlling the servo but the logic and programming behind the rest is proving too much for me.

Any and all help would be greatly appreciated.

Thanks in advance.

PeterH

Rather than try to determine whether either input is flashing, I would just look for whether it has been on recently. You can read the input, test whether it's active (i.e. LED on) or not. If it's active, record the current time as the time when it was last active. If it's not active, subtract the time it was last active from the current time to see how long ago it was last active. If that exceeds some multiple of your flash interval then the corresponding input signal is 'off', otherwise it's 'on'.

Do this separately for each of the input signals.

You can then use simple logic to compare the states of the two input signals and decide what the servo position should be.

The_Doc

Thanks for the reply... Unfortunately that leaves me even more confused than I was previously.

PeterH

OK, break the problem down. Do you know how to read an input?

The_Doc

#4
Sep 19, 2013, 03:01 pm Last Edit: Sep 19, 2013, 04:30 pm by The_Doc Reason: 1
I'm pretty much just fumbling my way through and picking up bits and pieces as I go.

I've been working on the servo positioner and this is what I have so far.


Code: [Select]
/*Two position servo control based on input state of pin 3

*/

int switchPin = 3; // Switch to control servo position
int switchState = 0; //Variable for the switch state
int servoPin = 2; // Signal pin for servo motor
int minPulse = 1000; // Minimum servo position
int maxPulse = 2000; // Maximum servo position
int pulse = 0; // Var for the pulse
int speed = 0; // Sets servo speed range 0 - 5

long lastPulse = 0; // Time in milliseconds of the last pulse
int refreshTime = 20; // Time between pulses

void setup() {
pinMode(servoPin, OUTPUT); // Initialise the servo signal pin as an OUTPUT
pulse = minPulse; // Set the servo position value to minimum specified value
pinMode(switchPin, INPUT); // Initialise the switchPin as an INPUT
//Serial.begin(9600); // Initialise the serial
}

void loop() {
switchState = digitalRead(switchPin); // Read the state of the switch
switch (switchState){
case 0:
if(pulse>minPulse){
pulse--;
delay(speed);
}
break;
case 1:
if(pulse<maxPulse){
pulse++;
delay(speed);
}else{
}
break;
}
movetheServo();
}

void movetheServo(){
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Servo to position 2
delayMicroseconds(pulse); // Length of the pulse sets the servo position
digitalWrite(servoPin, LOW); // Servo to position 1
lastPulse = millis(); // Save the time of the last pulse
}
}


It's the calculations and code to determine the input state when it's 'flashing' that I can't get my head around.

Thanks again and apologies for my lack of understanding... I had a limited grasp of BASIC about 30 years ago but it seems my brain no longer wants to cooperate.

The_Doc

It looks like I may also need to change the code for positioning the servo.

My first problem was that I didn't realise how sensitive the inputs are on the Arduino. The servo suddenly decided to start jumping all over the place and I eventually discovered the fix, with a pull down resistor on the input pin. Having cured that problem, I've discovered another. I don't know how best to describe it but the servo travels fine from position 1 to position 2, but as the servo initiates its travel back to position 1, it 'bounces' before continuing, then the same thing before it finishes travelling.
I have no idea what could be causing it but I presume it's something in the code.

The_Doc

#6
Sep 19, 2013, 07:22 pm Last Edit: Sep 19, 2013, 08:33 pm by The_Doc Reason: 1
Update...

Moving the servo to a different pin cured the bounce problem.

I've been using a Mega 2560 up until now but I've just written the code to a Pro Mini and noticed something very strange.

When I use:

int minPulse = 1000;
int maxPulse = 2000;

on the Pro Mini to control the servo limits, the servo only travels a fraction of the distance it did when connected to the Mega 2560.

I thought the limits were 550 and 2500, and that it would be the same no matter which flavour of Arduino the code was running on but in order to achieve the degree of servo movement I need, I've had to replace 1000 and 2000 with 1500 and 4000 respectively... does anyone know why this might be?

The_Doc

I'm still stuck on this. Can anyone give me a clue or an example of how I could go about this?

pmlapl

You perhaps already know this: Arduino has a servo library.
Check it out here: http://arduino.cc/en/Reference/Servo

It may solve your servo problem.

The_Doc


You perhaps already know this: Arduino has a servo library.
Check it out here: http://arduino.cc/en/Reference/Servo

It may solve your servo problem.


Thanks for that... I have decided to use the servo library as it looks like a much cleaner/easier solution. I'm rewriting the code at the moment and learning as I go.
I still haven't worked out how to read the pin states to distinguish between a solid and a flashing LED but I'll keep bashing at it and see where I get.

Thanks again.

Go Up