// "A Levitating Sphere Rotates Glows and Blinks" // Levitate with the very simple Bang-Bang control. // Supply 5-6V to the motor driver IC connected with the levitation coil. // Attach the Hall effect sensor "UGN3503UA" to the head of the core of the coil. // Stack 3 or 4 neodymium magnets (10-15mm Dia.). // Attach the N pole of the stacked magnets to the levitating object. (S pole up) // Copyright (C) 2016 ArduinoDeXXX All Rights Reserved. int setpoint = 650; int prevState = 0; int lastState = 0; int changes = 0; int timeActive = 0; int led = 32; int IN1_pin = PA7; boolean eState = false; void setup () { pinMode(led, OUTPUT); // led on STM32F103 "Blue Pill" pinMode(PA6, OUTPUT); pinMode(PA7, OUTPUT); timeActive = millis(); digitalWrite(IN1_pin, eState); digitalWrite(led, HIGH); //start with led off } void loop(){ if (prevState != lastState){ prevState=lastState; changes ++; // coil changed polarity } if ((millis()-timeActive) > 15000){ if (changes < 6){ eState = true; // disable H bridge IN1 pin and stay there forever digitalWrite(IN1_pin, eState); digitalWrite( led, LOW ); // onboard led. LOW = led on; HIGH = led off; STM can't source any current on this pin. } else{ timeActive= millis(); changes = 0; } } setpoint=analogRead(PA1); if (analogRead(PA0) > setpoint ){ // setpoint Adjust the threshold value to levitate. //GPIOA->regs->BSRR = (1 << 22); // clear PA6, all others no change. This is supposedly faster than digitalWrite. digitalWrite( PA6,LOW ); // coil fwd lastState=LOW; } else { //http://www.stm32duino.com/viewtopic.php?t=1220 //https://gist.github.com/iwalpola/6c36c9573fd322a268ce890a118571ca //GPIOA->regs->BSRR = (1 << 6);// set PA6, all others no change digitalWrite( PA6,HIGH ); // coil reverse lastState=HIGH; } }