I'm interested in trying to describe an oscillation by using an AC motor that does clockwise and counterclockwise. There are three wires. 2 of the wires control the direction.
I'm thinking of having each wire connected to a separate solid state relay for an arduino uno that would essentially power the related SSR on a direction, off, then the other SSR on and then off as a flicker. Would the following work?
My lack of understanding is the having the motor power on between 0-255. How does that relate to the duration? Or should I stick with HIGH and LOW?
For the code:
const int relayPinCC = 9; // set pin 8 for relay output counter clockwise
const int relayPinC = 10; // set pin 7 for relay output clockwise
int onDuration
int offDuration
void setup()
{
Serial.begin(9600);
pinMode(relayPinCC, OUTPUT);
pinMode(relayPinC, OUTPUT);
randomSeed(analogRead(0));
}
void loop()
{
onDuration = random (3000, 60000);
offDuration = random (3000,60000);
analogWrite (relayPinCC, random(0, 255));
analogWrite (relayPinCC, LOW);
analogWrite (relayPinC, random(0, 255));
analogWrite (relayPinC, LOW);
delay(onDuration);
analogWrite (relayPinCC, LOW);
analogWrite (relayPinC, LOW);
delay(offDuration);
}
}
Since it takes some time for the motor to react (inertia) you can probably control it simply by switching it on & off. But if you need precise position-control you'll need something to sense the position and use that as feedback to the software.
That probably won't work with most AC SSRs.
analogWrite() is PWM which switches on & off quickly to "simulate" analog. It can be used to control the speed of a DC motor, to dim a DC light bulb, or to make an LED appear dim.
But most AC SSRs are made with a TRIAC. Once a TRIAC is switched on, it latches-on until current drops to zero, which with AC is the next zero crossing. (If you use that type of SSR in a DC circuit it won't shut off until you remove power.)
A standard light dimmer uses a TRAIC by synchronizing to the AC cycle and switching it on part-way through each half-cycle and it remains latched-on until the end of the half-cycle (the next zero crossing). That's "similar" to PWM.
You should post the motor model and specs. Are you absolutely certain that you don't swap the two wires to change direction? If so, you need an H bridge, not separate relays to disconnect one lead or the other.
on/offDuration don't seem to be related to time and it looks like you're enabling both directions simultaneously
if you're trying to move the motor randomly in either direction for a random amount of time as well as stopping the motor for a random amount of time, shouldn't there also be a random variable that determines the direction?
shouldn't the appropriate relay depending on the direction be enabled for the onDuration, then disabled (motor off) for the offDuration?
I'm trying to have the motor flicker a clockwise, stop then counterclockwise stop that'll cause short burst of movement, then be delayed for a random amount of time.
If not, maybe I'm limited to only having it run one direction at time for a random duration.
The goal is have an indoor hanging art mobile that turns on its own by the wind or so.
I guess it's simliar to baby's hanging crib mobile.
I had hooked up the motor as a test and the reduction gear kept it slow so I won't need might to worry about turning off one relay to and turning the other one on to have it simulate a stutte. I decided to just control one rotational direction at time based on a random duration to keep it simple.
#include <Arduino.h>
const int relay1 = 10; // Solid State Relay 1 - Clockwise
const int relay2 = 9; // Solid State Relay 2 - Counter Clockwise
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
}
void loop() {
int duration1 = random (1000, 15000); // Random duration between 1 - 15 seconds
int duration2 = random (1000, 30000); // Random duration between 1 - 30 seconds
int option = random(0, 3); // runs an option - 0, 1, 2, or 3
switch (option) {
case 0:
digitalWrite(relay1, HIGH);
if (digitalRead(relay1) == HIGH) {
// If the first relay is running, don't run the second relay
digitalWrite(relay2, LOW);
delay(duration1);
break;
}
case 1:
digitalWrite(relay2, HIGH);
if (digitalRead(relay2) == HIGH) {
// If the second relay is running, don't run the first relay
digitalWrite(relay1, LOW);
delay(duration1);
break;
}
case 2:
digitalWrite(relay2, LOW);
digitalWrite(relay1, LOW);
delay(duration2);
break;
case 3:
digitalWrite(relay2, LOW);
digitalWrite(relay1, LOW);
delay(duration1);
break;
}
}