Medi

Dependencies:   mbed

main.cpp

Committer:
carlotara
Date:
2020-03-11
Revision:
1:b974ec8fe646
Parent:
0:6999a083fb46

File content as of revision 1:b974ec8fe646:

/*
############################################
##           sMotor v0.1 Test Program     ##
##          created by Samuel Matildes    ##
############################################
        ---- sam.naeec@gmail.com -----
This library was made for 4-Phase Stepper Motors
I don't take any resposability for the damage caused to your equipment.

*/

#include "mbed.h"



Serial pc(USBTX, USBRX);

//sMotor motor(D8, D9, D10, D11); // creates new stepper motor: IN1, IN2, IN3, IN4
//sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4
int step_speed = 1200 ; // set default motor speed
//int numstep = 512 ; // defines full turn of 360 degree
int numstep = 514
 ; // defines full turn of 360 degree
//you might want to calibrate this value according to your motor
int statomax =7;
//int kmax = 2;
//int statomax =7;
int kmax = 8;
int stato;
int ora = 1;
int data = 0;
//int statomax =7;


DigitalOut IN1(D8);
DigitalOut IN2(D9);
DigitalOut IN3(D10);
DigitalOut IN4(D11);

/*
const int  IN1 = 8;   // filo motore BLUE
const int  IN2 = 9;   // filo motore ROSA
const int  IN3 = 10;  // filo motore GIALLO
const int  IN4 = 11;  // filo motore ARANCIO
*/
const int MotoreOFF = 99; // motore spento



void Uscita( int i4,int i3,int i2,int i1)
{
    if (i1==1) IN1=1; else IN1=0;
    if (i2==1) IN2=1; else IN2=0;
    if (i3==1) IN3=1; else IN3=0;
    if (i4==1) IN4=1; else IN4=0;
/*
  if (i1==1) digitalWrite(IN1,HIGH); else digitalWrite(IN1,LOW);
  if (i2==1) digitalWrite(IN2,HIGH); else digitalWrite(IN2,LOW);
  if (i3==1) digitalWrite(IN3,HIGH); else digitalWrite(IN3,LOW);
  if (i4==1) digitalWrite(IN4,HIGH); else digitalWrite(IN4,LOW);
  */
}



void EseguiPasso(int stato)
{
int i1,i2,i3,i4;

  switch ( stato )
    {// vedi tabella nel pdf del motore passo passo
       case 0: Uscita(0,0,0,1); break;
       case 1: Uscita(0,0,1,1); break;
       case 2: Uscita(0,0,1,0); break; 
       case 3: Uscita(0,1,1,0); break;
       case 4: Uscita(0,1,0,0); break;
       case 5: Uscita(1,1,0,0); break;
       case 6: Uscita(1,0,0,0); break;
       case 7: Uscita(1,0,0,1); break;
       case 8: Uscita(0,0,0,1); break;
       case 9: Uscita(0,0,1,1); break;
       case 10: Uscita(0,0,1,0); break; 
       case 11: Uscita(0,1,1,0); break;
       case 12: Uscita(0,1,0,0); break;
       case 13: Uscita(1,1,0,0); break;
       case 14: Uscita(1,0,0,0); break;
//       case 7: Uscita(1,0,0,1); break;
       case MotoreOFF: //OFF
          Uscita(0,0,0,0); break;
    } 
            wait_ms(1);
 //  delay(1); //ritardo almeno 1 mS
}  

void RitardoAccensione()
{ //attesa prima di attivare il motore
  EseguiPasso(MotoreOFF); 
  for(int i=0; i<10; i++)
   {
//     digitalWrite(13,HIGH);
//    wait_ms(250);
 //    delay(250);
//     digitalWrite(13,LOW);
//    wait_ms(250);
  //   delay(250);
   } 
}  

int main()
{

    RitardoAccensione();  //2 secondi di pausa prima di iniziare
    while (1) {
        char kmaxCh;
        printf("Inserisci numero Settore \r\n");
 //       scanf("%c", &kmaxCh);
 //       printf("%c\n",kmaxCh);
        scanf("%d", &kmax);
        printf("%d\n",kmax);
//char numberstring[] = "42";
//int number;
 
//sscanf(numberstring, "%d", &number);        
//        kmax = 2;
        stato=0; //inizio da uno stato arbitrario


if (kmax>0)
{
  stato=0; //inizio da uno stato arbitrario

  for(int k=0; k<kmax; k++) //Rotazione Oraria
 // for(int k=0; k<4; k++) //Rotazione Oraria
    {
        for (int i=0; i<numstep; i++)  //45 gradi a ciclo
 //       for (int i=0; i<514; i++)  //45 gradi a ciclo
//    for (int i=0; i<1024; i++)  //90 gradi a ciclo
            { 
                EseguiPasso(stato);
                stato=stato+1; // avanza nella tabella
/*        printf("Stato : \r\n");
        printf("%d\r\n",stato);
        printf("k  : \r\n");
        printf("%d\r\n",k);
        */
                if ((stato)>statomax)
                    {
                        stato=0; 
 //       if ((stato)>7) stato=0; 
                    } 
                EseguiPasso(MotoreOFF);  
    //            wait_ms(500);  // pausa di mezzo secondo
            }  
        wait_ms(1000);  // pausa di 1 secondo
    }
  for(int k=0; k<kmax; k++) //Rotazione AntiOraria
//  for(int k=0; k<4; k++) //Rotazione AntiOraria
    {
        for (int i=0; i<numstep; i++)  //45 gradi a ciclo
//        for (int i=0; i<514; i++)  //45 gradi a ciclo
//    for (int i=0; i<1024; i++)  //90 gradi a ciclo
            { 
                EseguiPasso(stato);
                stato=stato-1; //torna indietro nella tabella
 /*       printf("Stato : \r\n");
        printf("%d\r\n",stato);
        printf("k  : \r\n");
        printf("%d\r\n",k);                
        */        
                if ((stato)<0)
                    {
                         stato=statomax; 
 //       if ((stato)<0) stato=7; 
                    } 
                EseguiPasso(MotoreOFF);  
       //         wait_ms(500); 
            }
    wait_ms(1000); 
    }
            }
 
}
}