A Place to share code for arduino/Pi
Posted: 22 Jan 2019, 20:06
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
Selfbondage Forum
https://forum.boundanna.net/board/
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
}
}
}
A flick of the magic whip and it's done.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.
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);
}
}
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);
}
}
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.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); } }
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.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.