Go Down

Topic: Electromagnetic Piston Engine. (Read 7 times) previous topic - next topic

Zanderist

Today at 04:02 am Last Edit: Today at 04:21 am by Zanderist
What I'm working on is a design project, it is supposed to be Electromagnetic piston.

My issue right now is figuring out the coding to get this led squence,
http://makeagif.com/i/Z38n90
The green dot is the position of the magnet
The Red dot is the active coil
The yellow dot is the direction of the theoretical H bridge.

Please not in the gif link that it appears that the coils are connected, they are not and they are firing individually.
This project is currently not meant to power anything, its just to represent the motion of the magnet in relation to the coils and direction LED.

Here is my code as of now,

Code: [Select]
//COIL LEDS
int coilLED [] ={2,3,4,5};
int currentcoilLED=0;
//MAGNET LEDS
int magnetLED[] ={6,7,8,9,10};
int currentmagnetLED=0;
//DIRECTION
int directionLED[]={11,12};
int direction =1;
int magdirection=1;
int coildirection=1;
//DELAY BETWEEN CHANGES
int ledDelay(1000);
unsigned long changeTime;

// the setup routine runs once when you press reset:
/*************************************************************/
void setup() {
  //COILLEDS
  for( int x =0; x<4; x++)
  {
 pinMode(coilLED[x], OUTPUT);
 
  }
  //MAGNETLEDS
 for( int x =0; x<5; x++)
  {
    pinMode(magnetLED[x], OUTPUT);
 }
 
 //DIRECTIONLED
 for( int x =0; x<2; x++)
  {
    pinMode(directionLED[x], OUTPUT);
  }
 //changeTime
 changeTime=millis();
/*****************************************************************/   
 
 
}

// the loop routine runs over and over again forever:
void loop() {
  //if statement
  if((millis()-changeTime)>ledDelay){
 //COILLEDS
  for( int x =0; x<4; x++)
    {
     digitalWrite(coilLED[x], LOW);
      }
//MAGNETLEDS
   for( int x =0; x<5; x++)
    {
      digitalWrite(magnetLED[x], LOW);
     
      }
  //RUN FUNCTIONS
   changeCOILLED();
   changeDIRECTIONLED();
   changeMAGNETLED();
 
  changeTime=millis();
}
}
/****************************************************************/
void changeCOILLED(){
 
     

  digitalWrite(coilLED[currentcoilLED],HIGH); //turn on current LED
  if(currentcoilLED== 3){  coildirection=-1;direction=-1;}
 
  currentcoilLED += coildirection;
 
  if(currentcoilLED == 0){  coildirection=1; direction=1;}
  //change direction at the end
 
   
 
   //increment
 
 }
/****************************************************************/
void changeDIRECTIONLED(){
   if(direction < 0)
   {
digitalWrite(directionLED[0],HIGH);
digitalWrite(directionLED[1],LOW);
   }
   if(direction > 0)
   {

digitalWrite(directionLED[0],LOW);
digitalWrite(directionLED[1],HIGH);
   }

}
/****************************************************************/
void changeMAGNETLED(){
  digitalWrite(magnetLED[currentmagnetLED],HIGH);
  if(direction > 0){
  if (currentcoilLED == 3) {currentmagnetLED=3;}
  if (currentcoilLED == 2) {currentmagnetLED=2;}
  if (currentcoilLED == 1) {currentmagnetLED=1;}
  if (currentcoilLED == 0) {currentmagnetLED=0;}
    }
    if(direction < 0){
  if (currentcoilLED == 3) {currentmagnetLED=4; }
  if (currentcoilLED == 2) {currentmagnetLED=3;}
  if (currentcoilLED == 1) {currentmagnetLED=2;}
  if (currentcoilLED == 0) {currentmagnetLED=1;}
    }
   
}

Go Up