First the diagram of the 2 magnets circuit.
Then this code
// pins
#define ENABLE1 12
#define POLARITY1a 11
#define POLARITY1b 10
#define ENABLE2 7
#define POLARITY2a 6
#define POLARITY2b 5
#define LED 13
// timing variables
unsigned long stap;
unsigned long currentMillis;
long previousMillis = 0;
const long interval = 100; // interval om de 1/10 seconde of 100 millisecs
// state for a blinking LED
int LEDSTATE = 0;
// variable states of magenet 1 and 2
// 0 = off, 1 = nord, 2 = south
int STATE1 = 0;
int STATE2 = 0;
// SETUP _______________________________________________________________
void setup() {
Serial.begin(9600);
pinMode(ENABLE1,OUTPUT);
pinMode(ENABLE2,OUTPUT);
pinMode(POLARITY1a,OUTPUT);
pinMode(POLARITY1b,OUTPUT);
pinMode(POLARITY2a,OUTPUT);
pinMode(POLARITY2b,OUTPUT);
pinMode(LED,OUTPUT);
}
// STATES ______________________________________________________________
void ONE(int state) {
if (state == 0) {
digitalWrite(ENABLE1,LOW);
digitalWrite(POLARITY1a,LOW);
digitalWrite(POLARITY1a,LOW);
} else if (state == 1) {
digitalWrite(ENABLE1,HIGH);
digitalWrite(POLARITY1a,HIGH);
digitalWrite(POLARITY1b,LOW);
} else if (state == 2) {
digitalWrite(ENABLE1,HIGH);
digitalWrite(POLARITY1a,LOW);
digitalWrite(POLARITY1b,HIGH);
}
}
void TWO(int state) {
if (state == 0) {
digitalWrite(ENABLE2,LOW);
digitalWrite(POLARITY2a,LOW);
digitalWrite(POLARITY2b,LOW);
} else if (state == 1) {
digitalWrite(ENABLE2,HIGH);
digitalWrite(POLARITY2a,HIGH);
digitalWrite(POLARITY2b,LOW);
} else if (state == 2) {
digitalWrite(ENABLE2,HIGH);
digitalWrite(POLARITY2a,LOW);
digitalWrite(POLARITY2b,HIGH);
}
}
// ACTIONS ______________________________________________________________
void LEDBLINK() {
if (LEDSTATE == 0)
LEDSTATE = 1;
else
LEDSTATE = 0;
digitalWrite(LED,LEDSTATE);
}
// alternating on / off, 'on' is rondom south or nord
void ONEBLINK() {
if (STATE1 == 0) {
STATE1 = int(random(1,2));
ONE(STATE1);
}
else {
STATE1 = 0;
ONE(0);
}
}
void TWOBLINK() {
if (STATE2 == 0) {
STATE2 = int(random(1,2));
TWO(STATE2);
}
else {
STATE2 = 0;
TWO(0);
}
}
//LOOPS_______________________________________________________________
void LOOP1(){ // magnet 1 nord, magnet 2 off
ONE(1);
TWO(0);
}
void LOOP2(){ // magnet 1 off, magnet 2 nord
ONE(0);
TWO(1);
}
void LOOP3(){ // magnet 1 south, magnet 2 off
ONE(2);
TWO(0);
}
void LOOP4(){ // magnet 1 off, magnet 2 south
ONE(0);
TWO(2);
}
void LOOP5(){
if (stap % 2 == 0){ // 1 stap op 2 aan or 1 step at 2 steps on
ONEBLINK();
}
if (stap % 8 == 0){ // 1 stap op 4 aan, 3 stappen op 4 uit or 1 step at 4 on, 3 steps at 4 off
TWOBLINK();
}
}
//MAIN LOOP____________________________________________________________
void loop() {
currentMillis = millis(); // sample the time
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
stap = stap+1;
LEDBLINK(); // niet zeker of dit hier mag staan!!!
//Serial.println(stap);
}
if (stap == 120) { // totaal aantal stappen per volledige loop or total steps for the whole loop
Serial.println("------- time reset --------");
stap = 0;
}
//deel 1
if (stap >=1 && stap<=10) {
Serial.println("LOOP1");
LOOP1();
}
//deel 2
if (stap >=11 && stap<=20) {
Serial.println("LOOP2");
LOOP2();
}
//deel 3
if (stap >=21 && stap<=120) {
Serial.println("LOOP3");
LOOP5();
}
}
Last a video of how my 2 coils look like and operate! This was controlled by a max patch and the same bench supply.
Now!! Why doesn't this work standalone on the Arduino. ERRORS are no movement of the magnet, bench supply clicking and fluctuating volt vs ampere's.
I've already put a cap 100v between the VCC2 and the Ground also no difference in fluctuating except for that the usb adapter doesn't give a red blink anymore, which is good.
