Skip to content

Instantly share code, notes, and snippets.

@jacobrosenthal
Created February 6, 2013 20:27
Show Gist options
  • Save jacobrosenthal/4725502 to your computer and use it in GitHub Desktop.
Save jacobrosenthal/4725502 to your computer and use it in GitHub Desktop.
Solenoid Test Arduino Sketch - really just a bit banged low frequency generator
int pin = 13;
int duty = 10; //duty cycle in percent - more is .. VERY power hungry,
int start_freq = 1; //in hz if you don't want to sweep but want constant, set equal to end_freq
int end_freq = 10;// in hz
int num_taps = 5;
int wait = 0; //delay in MS between sweeps
void setup(){
pinMode(pin, OUTPUT);
Serial.begin(9600);
}
void loop(){
int i = start_freq;
for(i; i<end_freq; i++)//range of hz to sweep through, resolution 1hz
{
for(int j=0; j<num_taps; j++)//how many times to initiate each hz
{
frequency(i);
}
}
delay(wait);
}
void frequency(int i){
//total frequency is i
//duty cycle is x% of i
int on=(10*duty)/i;
int freq=1000/i;
int off=freq-on;
digitalWrite(pin,HIGH);
delay(on);
Serial.print("On: ");
Serial.println(on);
digitalWrite(pin,LOW);
delay(off);
Serial.print("Off: ");
Serial.println(off);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment