A Place to share code for arduino/Pi

Selfbondage software and other kinky developments

Moderators: Riddle, Shannon SteelSlave

mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

A Place to share code for arduino/Pi

Post by mtlsub »

It would be great if those of you who ate gifted in the art of code would post code for projects. Possibly help clean up and help those of us who are just starting out :D
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

I'll hopefully get the ball rolling.

First I want to stress that this is the very first bit of code I have ever written so it wont be pretty and there may be better ways to do it. Be gentle Buuutt not too gentle :whip:

The following is code for a very simple stand alone blowjob trainer still in beta. The "button" Value is for testing only I will change the input to a photo resistor. I am also waiting on an display to make it fully stand alone.

The pin 13 is an electro-magnet. Pin 12 is wired to a shock collar. Pin 8 is a vibe.

once you set the timers for "time1" (punishment for taking too long) and "time2" (release) you then choose how many times the "button" must be pressed for release!
The magnet is only energized on the first press of the button and the program will simply run the timer down.
If you start sucking you better get the required # of sucks before your timers run down or you get a couple shocks for your trouble

Code: Select all

#include <SimpleTimer.h>                    //you need to look up SimpleTimer on Google and install the library according to the instructions. 
SimpleTimer timer;


int button=2;                               //defines pin 2 as button input
int val;                                    //defines the value of a pushed button
int count=11;                               //defines how many times button must be pushed [color=#FF0000]CHANGE # OF SUCKS[/color]
int press;                                  //logs each press
int relay2=13;                              //defines pin 13 as relay 2 if you don't want to use pin numbers 

void time1(){                               //triggered when timer of same name reaches 0
 relay2=digitalRead(13);                    //reads if relay 2 is HIGH or LOW
 if (relay2==LOW){                          //if relay 2 (pin 13) is LOW do the following
  Serial.println("TOO SLOW");               //triggered by relay 2 LOW  display TOO SLOW on computer
  digitalWrite(12,LOW);                     //triggered by relay 2 LOW  turns relay 1 on 
  delay(200);                               //triggered by relay 2 LOW  wait .2 seconds
  digitalWrite(12,HIGH);                    //triggered by relay 2 LOW  turns relay 1 off
  delay(2000);                              //triggered by relay 2 LOW  wait seconds
  digitalWrite(12,LOW);                     //triggered by relay 2 LOW  turns relay 1 on
  delay(200);                               //triggered by relay 2 LOW  wait .2 seconds
  digitalWrite(12,HIGH);                    //triggered by relay 2 LOW  turns relay 1 off
 } 
 }
 
void time2(){                               //triggered when timer of same name reaches 0
  digitalWrite(13,HIGH);                    //triggered by timer        turns relay 2 off 
  Serial.println("TIMER DONE");             //triggered by timer        display TIMER DONE on computer
  delay(20000);                             //triggered by timer        wait 20 seconds (stops anything interfering with release)
}


void setup() {
  Serial.begin(9600);                       //allows arduino to talk to computer
  digitalWrite(13,HIGH);                    //sets pin 13 to off to start
  digitalWrite(12,HIGH);                    //sets pin 12 to off to start
  digitalWrite(8,HIGH);                     //sets pin  8 to off to start
  pinMode(button,INPUT_PULLUP);             //sets input for push button
  pinMode(13,OUTPUT);                       //sets pin 13 as output (MAGNET)
  pinMode(12,OUTPUT);                       //sets pin 12 as output (SHOCK)
  pinMode(8,OUTPUT);                        //sets pin  8 as output (VIBE)
  timer.setTimeout(15000,time1);            //timer for timer 1        [color=#FF0000]change number to change time of punishment[/color]
  timer.setTimeout(25000,time2);            //timer for timer 2       [color=#FF0000] change number to change time of release[/color]
 

}
void loop() {
  timer.run();                              //starts timer 
  val=digitalRead(button);                  //reads pin 2 
  if (val==LOW){                            //if voltage is LOW 
    press=count--;                          //subtract 1 from the press count you can use press=count++ if you prefer to add presses you will need to modify other parts of the code.
    delay(400);                             //wait .4 seconds

  Serial.print("TIMES REMAINING = ");       //displays TIMES REMAINING on computer
  Serial.println(count);                    //adds the number of presses remaining to message
  if(count>=10 && count>0){                 //if the number of presses is greater or equal to 10 but higher than 0 ([color=#FF0000]this it what keeps the relay off before the button is pressed, change the 10 value for # of presses desired[/color])
  digitalWrite(13,LOW);                     //turn relay 2 on
}
  relay2=digitalRead(13);
  if(count==0 && relay2==LOW){              //when counter gets to 0 and wont trigger relay 3 if relay 2 is already off
  digitalWrite(8,LOW);                      //turns on relay 3 (OPTIONAL REWARD)
  Serial.println ("COUNTDOWN DONE!");       //displays COUNTDOWN DONE! on computer
  delay(10000);                             //keep relay 3 of for 10 seconds and delay turning off relay 2 [color=#FF0000]The longer this delay is the longer you get vibed before release[/color]
  digitalWrite(8,HIGH);                     //turns off relay 3
  digitalWrite(13,HIGH);                    //turns off relay 2 

}

} 
}
KinkInSpace
*****
Posts: 2141
Joined: 24 Dec 2015, 16:11
Location: Netherlands

Re: A Place to share code for arduino/Pi

Post by KinkInSpace »

Can you edit your post and change the quote BBcode to Code BBcode? This will preserve spaces and make everything a lot more readable. :)

Thanks. :)
Formally known as Slave_L.
I'm not yet very comfortable expressing my love for kink from my private life. I will therefor hide behind my username KinkInSpace and not allow any connections to who I really am. I'm sure you'll understand.
User avatar
bound_jenny
Moderator
Posts: 10268
Joined: 09 Dec 2007, 12:37
Location: Montreal, Canada, Great Kinky North

Re: A Place to share code for arduino/Pi

Post by bound_jenny »

Slave_L wrote:Can you edit your post and change the quote BBcode to Code BBcode? This will preserve spaces and make everything a lot more readable. :)
A flick of the magic whip and it's done. :hi:

Jenny.
Helplessness is a doorway to the innermost reaches of the soul.
If my corset isn't tight, it just isn't right!
Kink is the spice of life!
Come to the Dark Side - we have cookies!
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

Thanks! I was trying to post it like that but I was not sure how. :)
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

Ok, here is my second attempt.

The scenario starts when you press the START button.
Your electromagnetic lock engages.
a light is turned on telling you to suck.
when you suck your nose hits a button ( still have not bought photo-resistor)
if you suck fast enough your vibe turns on for a bit.
if you suck too slow you get a shock from a shock collar.
the light turns off. Repeat.
At the end of the session if you have sucked fast on average you get vibed and can cum then are released.
if you sucked slow on average you get shocked a couple more times then are released.

I want to get a bigger relay board because I want to make the dildo cum in your mouth at the end and you have to keep your mouth on the dildo while it does it. I still have to figure out the code for that.

Code: Select all

#define btnSTART 4 // Change these pins if you want to use different ones
#define btnHIT 2
#define ledTrigger 13
unsigned long startMillis;
unsigned long currentMillis;
int times = 50;
int maxTime = 60000;

void setup() {
  // setup the switches and LED
  pinMode(btnSTART, INPUT_PULLUP);
  pinMode(btnHIT, INPUT_PULLUP);
  pinMode(ledTrigger, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);

  // Turn off the LED
  digitalWrite(ledTrigger, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);

  // Start the Serial port
  Serial.begin(9600);
  Serial.println("Arduino Reaction Timer");
  Serial.println();
  // Randomize the random number generator
  randomSeed(analogRead(0));
  

}
void period1() {
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= 190000)  //test whether the period has elapsed{
    digitalWrite (8, HIGH);
    
  delay (100);
}




void loop() {
  long timeReaction;
  long timeTotal = 0;



  Serial.println("Press START button when ready");

  // Wait for the START button
  while (digitalRead(btnSTART)) {}
  // Then wait for it to be released
  delay(10);
  while (!digitalRead(btnSTART)) {}
  digitalWrite(8, LOW);
  delay(1000);
  startMillis = millis();


  // Start the testing round
  for (int i = 0; i < times; i++) {

    delay(random(300, 900));
    timeReaction = millis();
    digitalWrite(ledTrigger, LOW);

    // Wait for btnHit
    while (digitalRead(btnHIT)) {
      period1();
    }

    timeReaction = millis() - timeReaction;
    timeTotal += timeReaction;
    delay(10);
    while (!digitalRead(btnHIT)) {}
    digitalWrite(ledTrigger, HIGH);
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.print(timeReaction);
    Serial.println(" msec");
    delay(500);

    if (timeReaction <= 2000) {
      digitalWrite (12, LOW);
      delay (800);
      digitalWrite (12, HIGH);
    }
    if (timeReaction >= 2001) {
      digitalWrite (7, LOW);
      delay (500);
      digitalWrite (7, HIGH);


    }
  }
  // The set of 5 is done, show the results
  Serial.println();
  Serial.print("Avg = ");
  Serial.print(timeTotal / times);
  Serial.println(" msec");
  Serial.println();
  

  if (timeTotal / times >= 2001) {
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    delay (500);
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    digitalWrite (8, HIGH);
  }
  if (timeTotal / times <= 2000) {
    digitalWrite (12, LOW);
    delay (4000);
    digitalWrite (12, HIGH);
    digitalWrite (8, HIGH);
  }

}
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

Getting closer to my goal!
I have a working prototype that must be connected to the computer but my plan is to have the whole unit self contained. For that I need to get a screen and a way to input values. I also need to put everything in a project box.
So far My prototype delays start for you to get ready and prompts you to push a button to start your session
You get prompted to suck by LED and a message on screen to "SUCK"
Sucking on the dildo changes the resistance of the Photo-resistor and registers a suck
If you are fast enough you get a reward (quick buzz from a Hitachi) and you get the message "GOOD"
If you are too slow you get punished (zap from dog training collar) and the message "FASTER"
the screen also displays how many sucks you have done.
When you complete the set number of sucks the computer takes the average of your times and if it is fast enough the vibe stays on for a long time and then you are released. If you were always too slow you get a few extra shocks then are released.
There is a built in timer that will release you regardless of performance. There is no reward or punishment if this timer is reached you are simply released.
!!!!!REGARDLESS ALWAYS HAVE A SECOND AND EVEN A THIRD BACKUP RELEASE!!!!

Materials used;
Arduino Uno 3
4 channel relay
Photo-resistor module
clear dildo with hole drilled in base to get the photo-resistor in the middle and to mount the dildo on a tube
remote dog collar
push button
Electro-magnet
everything was purchased from Amazon except for the button that was purchased in the automotive section of Canadian Tire

Code: Select all

#define btnSTART 4 // Change these pins if you want to use different ones
#define btnHIT 2
#define ledTrigger 13
unsigned long startMillis;
unsigned long currentMillis;
int times = 50; // number of sucks
long maxTime = (120000); // Backup timer release

void setup() {
  // setup the switches and LED
  pinMode(btnSTART, INPUT_PULLUP);
  pinMode(btnHIT, INPUT);
  pinMode(ledTrigger, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);

  // Turn off the LED
  digitalWrite(ledTrigger, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);

  // Start the Serial port
  Serial.begin(9600);
  Serial.println("Arduino Suck Light");
  Serial.println();
  // Randomize the random number generator
  randomSeed(analogRead(0));
  

}
void period1() {
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= maxTime)  //test whether the period has elapsed{
    digitalWrite (8, HIGH); // Electro magnet release
    
  delay (100);
}




void loop() {
  long timeReaction;
  long timeTotal = 0;



  Serial.println("Press START button when ready");

  // Wait for the START button
  while (digitalRead(btnSTART)) {}
  // Then wait for it to be released
  delay(10);
  while (!digitalRead(btnSTART)) {}
  digitalWrite(8, LOW);
  delay(1000);
  startMillis = millis();


  // Start the testing round
  for (int i = 0; i < times; i++) {

    delay(random(300, 900));
    timeReaction = millis();
    digitalWrite(ledTrigger, LOW);
    Serial.println ("SUCK");

    // Wait for btnHit
    while (!digitalRead(btnHIT)) {
      period1();
    }

    timeReaction = millis() - timeReaction;
    timeTotal += timeReaction;
    delay(10);
    while (digitalRead(btnHIT)) {}
    digitalWrite(ledTrigger, HIGH);
    Serial.println(i + 1);
    //Serial.print(": "); used for debuging
    //Serial.print(timeReaction); used for debuging
    //Serial.println(" msec"); used for debuging
    delay(500);

    if (timeReaction <= 2000) {
      digitalWrite (12, LOW);
      Serial.println ("GOOD");
      delay (800);
      digitalWrite (12, HIGH);
    }
    if (timeReaction >= 2001) {
      digitalWrite (7, LOW);
      Serial.println ("FASTER");
      delay (500);
      digitalWrite (7, HIGH);


    }
  }
  // The set of 5 is done, show the results
  Serial.println();
  Serial.print("Avg = ");
  Serial.print(timeTotal / times);
  Serial.println(" msec");
  Serial.println();
  

  if (timeTotal / times >= 2001) {
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    delay (500);
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    digitalWrite (8, HIGH);
  }
  if (timeTotal / times <= 2000) {
    digitalWrite (12, LOW);
    delay (4000);
    digitalWrite (12, HIGH);
    digitalWrite (8, HIGH);
  }

}
schulz
*
Posts: 32
Joined: 08 Mar 2006, 16:15

Re: A Place to share code for arduino/Pi

Post by schulz »

Hi I'm realizing a WebControl for my Mistress, so she could control a 2 channel Tens, and inflatable buttplug and also a lock. I finished code and it's also working but sometimes there are some BUGs, could i Post the code and perhaps we go through it together, cause i don't find the faults anymore :-(
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

While I am more than willing to look at what you have I am FAR from a coding expert. There are however others on this forum who have some serious skills and they might see what you have and offer some advice.
User avatar
GeneralError
**
Posts: 141
Joined: 16 Sep 2019, 15:30
Location: Germany

Re: A Place to share code for arduino/Pi

Post by GeneralError »

Oh, just found your thread and saw that we found quite similar approaches to the same goal. :hi: Please see here viewtopic.php?f=28&t=12525#p90278 I plan to extend my system also to allow deep throat training enforced by a dog collar.
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

Your solution looks a bit more advanced than mine. Keep it up!

I hope someone can help me with this code ( i know it is messy but as I have said this is my first attempt at coding)
I have the times suck working and it knows if i sucked fast or slow. Currently when i suck slow it will print "Transmit" but it does not send a signal to the collar.
the code does send the signal before I attempted to merge the codes. I can't figure out what I did.

Code: Select all

byte buttonPin = 9;

//=================================================== START OF COLLAR SETUP CODE ======================================================================

//const int shock_min = 0; // Minimum of power a command will be executed at
const int shock_delay = 10; // Maximum rate at which the shock function can be used at
//const int cmd_max = 1000; // Maximum of milliseconds which a command can be executed at

// Constant variables
const int pin_led = LED_BUILTIN; // Pin for indication LED
const int pin_rtx =  12; // Pin to transmit over
const String key = "00101100101001010"; // Key of the transmitter, dont touch if you dont know how it works

// Variables which do change
int collar_chan = 0; // Can be channel 0 or 1
int collar_duration = 500; // Duration of the command in milliseconds
int collar_power = 10; // Strength of the command, can be 0-100, but will be limited by shock_min and shock_max

// Define values for easier recognition
#define COLLAR_LED 1
#define COLLAR_BEEP 2
#define COLLAR_VIB 3
#define COLLAR_ZAP 4

// Strings used for building up the command sequence
String sequence, power, channelnorm, channelinv, modenorm, modeinv;

// Store the last time anything was transmitted to the collar
unsigned long transmit_last = 0;
unsigned long shock_last = 0;
void transmit_command(int c, int m, int p = 0)
{
  transmit_last = millis();
  switch (c) // Check the channel
  {
    case 1: // Channel 1
      channelnorm = "111";
      channelinv = "000";
      break;
    default: // Channel 0
      channelnorm = "000";
      channelinv = "111";
      break;
  }

  switch (m) // Check the mode
  {
    case 1: // Light
      modenorm = "1000";
      modeinv = "1110";
      break;
    case 2: // Beep
      modenorm = "0100";
      modeinv = "1101";
      break;
    case 4: // Shock
      modenorm = "0001";
      modeinv = "0111";
      shock_last = millis();
      break;
    default: // Vibrate
      modenorm = "0010";
      modeinv = "1011";
//      p = 10; // Set strengh to 10 for the command to be executed properly
      break;
  }

  // Convert power to binary
  int zeros = String(p, BIN).length();

  String power;
  for (int i = 0; i < 7 - zeros; i++)
  {
    power = power + "0";
  }
  power = power + String(p, BIN);

  String sequence = "1" + channelnorm + modenorm + key + power + modeinv + channelinv + "00";

  digitalWrite(pin_led, LOW);
//  d = constrain(d, 50, cmd_max); // Clamp duration of the command
  unsigned long cmd_start = millis();
//  while (millis() - cmd_start < d)
//  {
    // start bit
    digitalWrite(pin_rtx, HIGH);
    delayMicroseconds(1500); // wait 1500 uS
    digitalWrite(pin_rtx, LOW);
    delayMicroseconds(741);// wait 741 uS

    for (int n = 0; n < 41 ; n++)
    {
      if (sequence.charAt(n) == '1') // Transmit a one
      {
        digitalWrite(pin_rtx, HIGH);
        delayMicroseconds(741);
        digitalWrite(pin_rtx, LOW);
        delayMicroseconds(247);
      }
      else // Transmit a zero
      {
        digitalWrite(pin_rtx, HIGH);
        delayMicroseconds(247);
        digitalWrite(pin_rtx, LOW);
        delayMicroseconds(741);
      }
    }
    delayMicroseconds(4500);
//  }
  digitalWrite(pin_led, HIGH);
}

void collar_keepalive()
{
  if (millis() - transmit_last >= 15000) // Send command to the collar at least every 2 minutes to make it stay on
  {
    Serial.println("Keep-alive:\tCollar");
    transmit_command(collar_chan, COLLAR_LED, 50);
  }
}
void sendit()
   {
     Serial.println("transmit");
    transmit_command(collar_chan, COLLAR_VIB, 50);
    delay(200);
   } 


//=================================================== END OF COLLAR SETUP CODE ======================================================================
//==================================================== start of light setup code ====================================================================

#define btnSTART 4 // Change these pins if you want to use different ones
#define btnHIT 2
#define ledTrigger 13
unsigned long startMillis;
unsigned long currentMillis;
int times = 30;
long maxTime = (240000);


  
//======================================================= end of light setup =======================================================================

void setup()
{
//=================================================== START OF COLLAR SETUP CODE ======================================================================
  pinMode(pin_rtx, OUTPUT); // Set transmitter pin as output
  pinMode(pin_led, OUTPUT); // Set LED pin as output
  
  
  
  //=================================================== END OF COLLAR SETUP CODE ======================================================================
 
  // setup the switches and LED
  pinMode(btnSTART, INPUT_PULLUP);
  pinMode(btnHIT, INPUT);
  pinMode(ledTrigger, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, INPUT_PULLUP);
  
  // Turn off the LED
  digitalWrite(ledTrigger, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);

  // Start the Serial port
  Serial.begin(9600);
  Serial.println("Arduino Suck Light");
  Serial.println();
  // Randomize the random number generator
  randomSeed(analogRead(0));
  

}
void period1() {
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= maxTime)  //test whether the period has elapsed{
    digitalWrite (8, HIGH);
    
  delay (100);

//========================================================end light ===============================================================================  

}

void loop()
{
  
 
  //if (digitalRead(9) == HIGH) {
  //  sendit();
  //Serial.write("button pressed");// test if button works must change serial rate from 115200 to 9600
 // }
  long timeReaction;
  long timeTotal = 0;



  Serial.println("Press START button when ready");

  // Wait for the START button
  while (digitalRead(btnSTART)) {}
  // Then wait for it to be released
  delay(10);
  while (!digitalRead(btnSTART)) {}
  digitalWrite(8, LOW);
  delay(1000);
  startMillis = millis();


  // Start the testing round
  for (int i = 0; i < times; i++) {

    delay(random(300, 900));
    timeReaction = millis();
    digitalWrite(ledTrigger, LOW);
    Serial.println ("SUCK");

    // Wait for btnHit
    while (!digitalRead(btnHIT)) {
      period1();
      collar_keepalive();
    }

    timeReaction = millis() - timeReaction;
    timeTotal += timeReaction;
    delay(10);
    while (digitalRead(btnHIT)) {}
    digitalWrite(ledTrigger, HIGH);
    Serial.println(i + 1);
    //Serial.print(": "); used for debuging
    //Serial.print(timeReaction); used for debuging
    //Serial.println(" msec"); used for debuging
    delay(500);

    if (timeReaction <= 2000) {
      digitalWrite (12, LOW);
      Serial.println ("GOOD");
      delay (1000);
      digitalWrite (12, HIGH);
    }
    if (timeReaction >= 2001) {
      sendit();
     
      
  }
  }
  // The set is done, show the results
  Serial.println();
  Serial.print("Avg = ");
  Serial.print(timeTotal / times);
  Serial.println(" msec");
  Serial.println();
  

  if (timeTotal / times >= 2001) {
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    delay (500);
    
    digitalWrite (8, HIGH);
  }
  if (timeTotal / times <= 2000) {
    digitalWrite (12, LOW);
    Serial.println ("GOOD JOB! CAN YOU LAST 10 SECONDS?");
    delay (10000);
    digitalWrite (12, HIGH);
    digitalWrite (8, HIGH);
  
    
  }
}
User avatar
Riddle
****
Posts: 1136
Joined: 24 Sep 2008, 08:37
Location: Oregon, USA
Contact:

Re: A Place to share code for arduino/Pi

Post by Riddle »

mtlsub wrote:Your solution looks a bit more advanced than mine. Keep it up!

I hope someone can help me with this code ( i know it is messy but as I have said this is my first attempt at coding)
I have the times suck working and it knows if i sucked fast or slow. Currently when i suck slow it will print "Transmit" but it does not send a signal to the collar.
the code does send the signal before I attempted to merge the codes. I can't figure out what I did.

Code: Select all

byte buttonPin = 9;

//=================================================== START OF COLLAR SETUP CODE ======================================================================

//const int shock_min = 0; // Minimum of power a command will be executed at
const int shock_delay = 10; // Maximum rate at which the shock function can be used at
//const int cmd_max = 1000; // Maximum of milliseconds which a command can be executed at

// Constant variables
const int pin_led = LED_BUILTIN; // Pin for indication LED
const int pin_rtx =  12; // Pin to transmit over
const String key = "00101100101001010"; // Key of the transmitter, dont touch if you dont know how it works

// Variables which do change
int collar_chan = 0; // Can be channel 0 or 1
int collar_duration = 500; // Duration of the command in milliseconds
int collar_power = 10; // Strength of the command, can be 0-100, but will be limited by shock_min and shock_max

// Define values for easier recognition
#define COLLAR_LED 1
#define COLLAR_BEEP 2
#define COLLAR_VIB 3
#define COLLAR_ZAP 4

// Strings used for building up the command sequence
String sequence, power, channelnorm, channelinv, modenorm, modeinv;

// Store the last time anything was transmitted to the collar
unsigned long transmit_last = 0;
unsigned long shock_last = 0;
void transmit_command(int c, int m, int p = 0)
{
  transmit_last = millis();
  switch (c) // Check the channel
  {
    case 1: // Channel 1
      channelnorm = "111";
      channelinv = "000";
      break;
    default: // Channel 0
      channelnorm = "000";
      channelinv = "111";
      break;
  }

  switch (m) // Check the mode
  {
    case 1: // Light
      modenorm = "1000";
      modeinv = "1110";
      break;
    case 2: // Beep
      modenorm = "0100";
      modeinv = "1101";
      break;
    case 4: // Shock
      modenorm = "0001";
      modeinv = "0111";
      shock_last = millis();
      break;
    default: // Vibrate
      modenorm = "0010";
      modeinv = "1011";
//      p = 10; // Set strengh to 10 for the command to be executed properly
      break;
  }

  // Convert power to binary
  int zeros = String(p, BIN).length();

  String power;
  for (int i = 0; i < 7 - zeros; i++)
  {
    power = power + "0";
  }
  power = power + String(p, BIN);

  String sequence = "1" + channelnorm + modenorm + key + power + modeinv + channelinv + "00";

  digitalWrite(pin_led, LOW);
//  d = constrain(d, 50, cmd_max); // Clamp duration of the command
  unsigned long cmd_start = millis();
//  while (millis() - cmd_start < d)
//  {
    // start bit
    digitalWrite(pin_rtx, HIGH);
    delayMicroseconds(1500); // wait 1500 uS
    digitalWrite(pin_rtx, LOW);
    delayMicroseconds(741);// wait 741 uS

    for (int n = 0; n < 41 ; n++)
    {
      if (sequence.charAt(n) == '1') // Transmit a one
      {
        digitalWrite(pin_rtx, HIGH);
        delayMicroseconds(741);
        digitalWrite(pin_rtx, LOW);
        delayMicroseconds(247);
      }
      else // Transmit a zero
      {
        digitalWrite(pin_rtx, HIGH);
        delayMicroseconds(247);
        digitalWrite(pin_rtx, LOW);
        delayMicroseconds(741);
      }
    }
    delayMicroseconds(4500);
//  }
  digitalWrite(pin_led, HIGH);
}

void collar_keepalive()
{
  if (millis() - transmit_last >= 15000) // Send command to the collar at least every 2 minutes to make it stay on
  {
    Serial.println("Keep-alive:\tCollar");
    transmit_command(collar_chan, COLLAR_LED, 50);
  }
}
void sendit()
   {
     Serial.println("transmit");
    transmit_command(collar_chan, COLLAR_VIB, 50);
    delay(200);
   } 


//=================================================== END OF COLLAR SETUP CODE ======================================================================
//==================================================== start of light setup code ====================================================================

#define btnSTART 4 // Change these pins if you want to use different ones
#define btnHIT 2
#define ledTrigger 13
unsigned long startMillis;
unsigned long currentMillis;
int times = 30;
long maxTime = (240000);


  
//======================================================= end of light setup =======================================================================

void setup()
{
//=================================================== START OF COLLAR SETUP CODE ======================================================================
  pinMode(pin_rtx, OUTPUT); // Set transmitter pin as output
  pinMode(pin_led, OUTPUT); // Set LED pin as output
  
  
  
  //=================================================== END OF COLLAR SETUP CODE ======================================================================
 
  // setup the switches and LED
  pinMode(btnSTART, INPUT_PULLUP);
  pinMode(btnHIT, INPUT);
  pinMode(ledTrigger, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, INPUT_PULLUP);
  
  // Turn off the LED
  digitalWrite(ledTrigger, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);

  // Start the Serial port
  Serial.begin(9600);
  Serial.println("Arduino Suck Light");
  Serial.println();
  // Randomize the random number generator
  randomSeed(analogRead(0));
  

}
void period1() {
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= maxTime)  //test whether the period has elapsed{
    digitalWrite (8, HIGH);
    
  delay (100);

//========================================================end light ===============================================================================  

}

void loop()
{
  
 
  //if (digitalRead(9) == HIGH) {
  //  sendit();
  //Serial.write("button pressed");// test if button works must change serial rate from 115200 to 9600
 // }
  long timeReaction;
  long timeTotal = 0;



  Serial.println("Press START button when ready");

  // Wait for the START button
  while (digitalRead(btnSTART)) {}
  // Then wait for it to be released
  delay(10);
  while (!digitalRead(btnSTART)) {}
  digitalWrite(8, LOW);
  delay(1000);
  startMillis = millis();


  // Start the testing round
  for (int i = 0; i < times; i++) {

    delay(random(300, 900));
    timeReaction = millis();
    digitalWrite(ledTrigger, LOW);
    Serial.println ("SUCK");

    // Wait for btnHit
    while (!digitalRead(btnHIT)) {
      period1();
      collar_keepalive();
    }

    timeReaction = millis() - timeReaction;
    timeTotal += timeReaction;
    delay(10);
    while (digitalRead(btnHIT)) {}
    digitalWrite(ledTrigger, HIGH);
    Serial.println(i + 1);
    //Serial.print(": "); used for debuging
    //Serial.print(timeReaction); used for debuging
    //Serial.println(" msec"); used for debuging
    delay(500);

    if (timeReaction <= 2000) {
      digitalWrite (12, LOW);
      Serial.println ("GOOD");
      delay (1000);
      digitalWrite (12, HIGH);
    }
    if (timeReaction >= 2001) {
      sendit();
     
      
  }
  }
  // The set is done, show the results
  Serial.println();
  Serial.print("Avg = ");
  Serial.print(timeTotal / times);
  Serial.println(" msec");
  Serial.println();
  

  if (timeTotal / times >= 2001) {
    digitalWrite (7, LOW);
    delay (500);
    digitalWrite (7, HIGH);
    delay (500);
    
    digitalWrite (8, HIGH);
  }
  if (timeTotal / times <= 2000) {
    digitalWrite (12, LOW);
    Serial.println ("GOOD JOB! CAN YOU LAST 10 SECONDS?");
    delay (10000);
    digitalWrite (12, HIGH);
    digitalWrite (8, HIGH);
  
    
  }
}
Having trouble reading your code on my tablet. Will try reading it on a computer later.

Edit: can you post the working transmit code?
Resident timer maker. :hi:
Let’s make timers together!
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

Want to make sure I give credit where credit is due. Here is the link http://deviant-designs.co.uk/2019/03/29 ... ck-collar/
User avatar
Riddle
****
Posts: 1136
Joined: 24 Sep 2008, 08:37
Location: Oregon, USA
Contact:

Re: A Place to share code for arduino/Pi

Post by Riddle »

mtlsub wrote:I hope someone can help me with this code ( i know it is messy but as I have said this is my first attempt at coding)
I have the times suck working and it knows if i sucked fast or slow. Currently when i suck slow it will print "Transmit" but it does not send a signal to the collar.
the code does send the signal before I attempted to merge the codes. I can't figure out what I did.
I believe that your light code has too many long delays and the keep alive function is not working any more. The collar needs a signal every so often to keep it turned on and functioning. This is not happening so the collar is turning off and not receiving any commands.

Edit: If you have an extra Arduino, you can dedicate 1 for the shock collar and one for the light program. Connect the 2 Arduino's together using SPI with the light Arduino as master and the collar Arduino as slave.

If you do not have an extra Arduino, we will have to rework your light code.
Resident timer maker. :hi:
Let’s make timers together!
mtlsub
**
Posts: 170
Joined: 12 Oct 2010, 05:23
Location: Great kinky North

Re: A Place to share code for arduino/Pi

Post by mtlsub »

I thought that might be the case however even if I fail the first suck it does not transmit. Regardless I will work on getting rid of some of the delays and seeing what that does. Thanks for taking a look. I'll take all the help I can get :D
Post Reply