Blog 26

From BitWizard Wiki
Jump to navigation Jump to search


Hardware used:

Hello,

In this project I worked on the arduino. I made an pulse controller. With that you can control the amount of pulses and the pulse time of the relay. Link to the code: click here

I made this script building on the arduino spi demo script. The parts I am going to explain are only for my script part.

In the script itself I also added some explanation.

Setup part:

//int offms = 500;
int OnTime = 500;
unsigned int s = 0;
int pulses = 1; 
int PulseTime = 500; 
 set_var (0x8e, 0x10, 0x00);
 PulsPrint(pulses);
 TimePrint(PulseTime);
 char bufline2[32];
 char bufline4[32];
 
 sprintf(bufline2, "1=down |2=up |5=res", bufline2);    
 write_at_lcd (0, 1, bufline2);
 
 delay(10);
 sprintf(bufline4, "3=down |4=up |6=run", bufline4);    
 write_at_lcd (0, 3, bufline4);
 
 Serial.write (bufline2);
 Serial.write ("\r\n");
 Serial.write (bufline4);
 Serial.write ("\r\n");


loop part:

  // read the buttons:
 int Button = get_var (0x94, 0x31);
 // print out the state of the buttons:
 Serial.println(Button);
 
 //Button 6
 //runs the pulses with the given pulse time to the relay
 if (Button == 1){
      Pulsing(pulses);          
      get_var (0x94, 0x31);
 }
 
 // Button 5 
 // reset the pulses and time back to the start settings
 if (Button == 2){
   PulseTime = 500;
   pulses = 1;    
   TimePrint(PulseTime);
   PulsPrint(pulses);
   delay(OnTime);
   get_var (0x94, 0x31);
 }
 // button 4 
 // +0.1 second to the previous amount of time
 // the maximum amount of seconds is 10
 if (Button == 4){
   PulseTime = PulseTime + 100; 
   if ( PulseTime > 10000) PulseTime = 10000; 
   TimePrint(PulseTime);
   delay(OnTime);
   get_var (0x94, 0x31);
 }
 
 // button 3 
 // -0.1second to the previous amount of time
 // The minimum amount of seconds is 0.1. 
 if (Button == 8){
   PulseTime = PulseTime - 100;
   if ( PulseTime < 100) PulseTime = 100; 
   TimePrint(PulseTime);
   delay(OnTime);
   get_var (0x94, 0x31);
 }
 // button 2 
 // +1 to the previous value pulses  
 // the maximum amount of pulses is 99
 if (Button == 16){
   pulses = pulses +1;
   if ( pulses > 99) pulses = 99; 
   PulsPrint(pulses);
   delay(OnTime);
   get_var (0x94, 0x31);
 }
 
 // button 1 
 // -1 to the previous value pulses  
 // the minimum value is 1
 if (Button == 32){
   pulses = pulses - 1;
   if ( pulses < 1) pulses = 1; 
   PulsPrint(pulses);
   delay(OnTime);
   get_var (0x94, 0x31);
 }
 delay(20);
}

functions:

//function that shows the amount of pulses on the display
void PulsPrint(int Puls){
 char buf[32];
 sprintf (buf, "Pulses: %02d", Puls);
 write_at_lcd (0, 0, buf);
 Serial.write (buf);
 Serial.write ("\r\n"); 
}
// function that shows the pulsetime on the display
void TimePrint(int Time){
 float FloatTime = Time;
 FloatTime = FloatTime/1000;
 
 delay(10);
 char TextBuf[32];
 char buf[32];
 sprintf(TextBuf, "Time:      seconds", buf);    
 write_at_lcd (0, 2, TextBuf);
 
 dtostrf (FloatTime,3, 1, buf);
 write_at_lcd (7, 2, buf);
 Serial.write (TextBuf);
 Serial.write (buf);
 Serial.write ("\r\n");
}
// function that will run the given pulses and pulsetime
void Pulsing(int Puls){
int PulsHalf = PulseTime / 2;
 for (int count = 0; pulses > count; count++){ 
       set_var (0x8e, 0x20, 0x01);
       delay(PulsHalf);
       set_var (0x8e, 0x20, 0x00);      
       delay(PulsHalf);
}
}


Useful links