Could I potentially make the delay in my code a variable that could be switched based on button presses?
IE =
one button press = delay 1000
Two button press = delay 2000
I've looked for a while and I can't find an example of anyone doing this, is it possible?
What happens if you press a button once, but twice? 
Delta_G:
You certainly can. Have you tried? What did your try look like.
int delayTime = 1000;
// some code to adjust the value of delayTime however you want
delay(delayTime);
I have not tried as i thought it wasnt possible..
what do you mean by (some code to adjust value of delaytime)
#include <Manchester.h>
#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect
const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch
int delayTime = 1000;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;
boolean onState = false;
boolean onStatetwo = false;
void setup() {
man.setupTransmit(TX_PIN, MAN_1200);
pinMode(hallPin, INPUT);
pinMode(TILT, INPUT);
pinMode(button, INPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
[b]// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
[u]Here instead of digitalWrite to turn led on what would i use to change a variable?[/u]
[/b]
int newHallState = digitalRead(hallPin);
// if different state and in proximity
if( (newHallState != hallState) && (newHallState == LOW))
{
// flip on/off state
onState = !onState;
if (onState)
{
man.transmit(transmit_data);
man.transmit(transmit_t);
} else {
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
// save new state
hallState = newHallState;
/////////////////////////////
int newTiltState = digitalRead(TILT);
// if different state and in proximity
if( (newTiltState != TILTState) && (newTiltState == LOW))
{
// flip on/off state
onStatetwo = !onStatetwo;
if (onStatetwo)
{
man.transmit(transmit_data);
man.transmit(transmit_t);
} else {
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
// save new state
TILTState = newTiltState;
}
Delta_G:
I mean write some code that reads a button press and changes the value of delayTime variable, or possibly reads a pot and adjusts it or whatever you want to do to change the value in that variable.
If that is beyond you, then you need to go back and work through a few tutorials and examples and try to familiarize yourself with the language.
What examples could i read in order to familiarize myself with this?
ive read that example lots of times
would this work?
if (buttonPushCounter % 1 == 0) {
int delayTime = 1000;
}
if (buttonPushCounter % 2 == 0) {
int delayTime = 2000;
}
if (buttonPushCounter % 3 == 0) {
int delayTime = 3000;
}
is it really that simple?
Delta_G:
Every time you put the word "int" there, you are creating a new variable with the same name as one you already have. It only exists between the { and } where it was created. So lose the "int" in front of those and yes, something like that would work.
An alternative to replace all of that would be:
delayTime = buttonPushCounter * 1000;
So i tried taking the int off
if (buttonPushCounter % 1 == 0) {
delayTime = 1000;
}
if (buttonPushCounter % 2 == 0) {
delayTime = 3000;
}
if (buttonPushCounter % 3 == 0) {
delayTime = 5000;
}
if (buttonPushCounter % 4 == 0) {
delayTime = 10;
}
it doesnt work though.
what doesnt make sense is where would i place
delayTime = buttonPushCounter * 1000;
?
Here is my full code.
#include <Manchester.h>
#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect
const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch
int delayTime = 5;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;
boolean onState = false;
boolean onStatetwo = false;
void setup() {
man.setupTransmit(TX_PIN, MAN_1200);
pinMode(hallPin, INPUT);
pinMode(TILT, INPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 1 == 0) {
delayTime = 1000;
}
if (buttonPushCounter % 2 == 0) {
delayTime = 3000;
}
if (buttonPushCounter % 3 == 0) {
delayTime = 5000;
}
if (buttonPushCounter % 4 == 0) {
delayTime = 10;
}
int newHallState = digitalRead(hallPin);
// if different state and in proximity
if( (newHallState != hallState) && (newHallState == LOW) )
{
// flip on/off state
onState = !onState;
if (onState)
{ delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
} else {
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
// save new state
hallState = newHallState;
/////////////////////////////
int newTiltState = digitalRead(TILT);
// if different state and in proximity
if( (newTiltState != TILTState) && (newTiltState == LOW))
{
// flip on/off state
onStatetwo = !onStatetwo;
if (onStatetwo)
{ delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
} else {
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
// save new state
TILTState = newTiltState;
}
well i guess i should explain what my project does first.
I have 2 atttiny85's one is the tx and the latter is rx (they both are connected via manchester encoding and 433 mhz modules)
the tx has a hall effect sensor and a mercury switch
when there is a magnet present it turns led on rx and stays on till magnet is present again (state change detection)
same thing for the mercury switch.
I coded in a delay for my project before and liked it.
when a magnet was present it would wait about 3 seconds then send the data and then rx would turn on led. I loved it but once im done with this project i want to be able to click a button that changes the delay time and i will probably hook up an RGB led that lights up a certain color for a second to match the certain delay time. so i'll know when i use the mercury switch or hall effect i'll know how long the delay will be for.
right now my code does the same thing it did before i added the delay code which is have no delay no matter how many times the button is pressed so the code did nothing because i prob messed up and did something wrong. lol
Delta_G:
When the code is in a delay, it can't read button presses. It can't do anything. delay is like putting your chip into a coma for a period of time. If you have a 3 seconds delay in your code and you press your button during that time, nothing will happen because the chip cannot see the button press. It is busy delaying.
If you want to be able to respond to button presses, then you have to get rid of the delay and embrace the technique used in the Blink Without Delay example to handle timing of things by recording the time something happens and calculating later if it is time to do it again or not. That way the rest of your code can continue running and responding to things like button presses.
Delta_G:
When the code is in a delay, it can't read button presses. It can't do anything. delay is like putting your chip into a coma for a period of time. If you have a 3 seconds delay in your code and you press your button during that time, nothing will happen because the chip cannot see the button press. It is busy delaying.
If you want to be able to respond to button presses, then you have to get rid of the delay and embrace the technique used in the Blink Without Delay example to handle timing of things by recording the time something happens and calculating later if it is time to do it again or not. That way the rest of your code can continue running and responding to things like button presses.
its not going to delay while im pressing the button. the whole idea was to change the variable of the delay when i press the button not actually delay while pressing the button. if you understood my sketch and idea you would then know that delay would only be activated while the mercury switch or hall effect sensor were activated. the button is merely a tool used to change the delay time for it.
so to recap its not working, not because delay is blocking ( i understand that delay causes the ic to freeze)
but its not working because i wrote the code wrong or placed it in the wrong spot.
Wesley5n1p35:
its not going to delay while im pressing the button. the whole idea was to change the variable of the delay when i press the button not actually delay while pressing the button. if you understood my sketch and idea you would then know that delay would only be activated while the mercury switch or hall effect sensor were activated. the button is merely a tool used to change the delay time for it.
so to recap its not working, not because delay is blocking ( i understand that delay causes the ic to freeze)
but its not working because i wrote the code wrong or placed it in the wrong spot.
I think you are saying that you want to advance the delay with each button push... a sort of State Machine. Here is your code that will do that (untested) but again, the button press will not be 'noticed' by the program during your blocking delays... only when that delay is not active.
#include <Manchester.h>
#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect
const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch
int delayTime = 5;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;
boolean onState = false;
boolean onStatetwo = false
byte state = 0;
void setup()
{
Serial.begin(9600); //<<<<<<<<<<<<<<< added in edit
man.setupTransmit(TX_PIN, MAN_1200);
pinMode(hallPin, INPUT);
pinMode(TILT, INPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
state++;
if (state > 3)
{
state = 0;
}
}
switch (state)
{
case 0:
delayTime = 1000;
break;
case 1:
delayTime = 3000;
break;
case 2:
delayTime = 5000;
break;
case 3;
delayTime = 10;
break
}
Serial.print("new delay time= ");
Serial.println(delayTime);
delay(50);
}
lastButtonState = buttonState;
int newHallState = digitalRead(hallPin);
if (newHallState != hallState && newHallState == LOW)
{
onState = !onState;
if (onState)
{
delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
}
else
{
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
hallState = newHallState;
int newTiltState = digitalRead(TILT);
if (newTiltState != TILTState && newTiltState == LOW)
{
onStatetwo = !onStatetwo;
if (onStatetwo)
{
delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
}
else
{
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
TILTState = newTiltState;
}
T
he next step of improving your code would be to make all this happen without the delay() function...
#include <Manchester.h>
#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect
const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch
int delayTime = 5;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;
boolean onState = false;
boolean onStatetwo = false;
byte state = 0;
void setup()
{
man.setupTransmit(TX_PIN, MAN_1200);
pinMode(hallPin, INPUT);
pinMode(TILT, INPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
state++;
if (state > 3)
{
state = 0;
}
}
switch (state)
{
case 0:
delayTime = 1000;
break;
case 1:
delayTime = 3000;
break;
case 2:
delayTime = 5000;
break;
case 3:
delayTime = 10;
break;
}
Serial.print("new delay time= ");
Serial.println(delayTime);
delay(50);
}
lastButtonState = buttonState;
int newHallState = digitalRead(hallPin);
if (newHallState != hallState && newHallState == LOW)
{
onState = !onState;
if (onState)
{
delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
}
else
{
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
hallState = newHallState;
int newTiltState = digitalRead(TILT);
if (newTiltState != TILTState && newTiltState == LOW)
{
onStatetwo = !onStatetwo;
if (onStatetwo)
{
delay(delayTime);
man.transmit(transmit_data);
man.transmit(transmit_t);
}
else
{
man.transmit(transmit_y);
man.transmit(transmit_g);
}
}
TILTState = newTiltState;
}
cleaned up a few spots that had were wrong and were not compiling, compiles just fine now. however no matter how many button presses it gives me about 3 second delay.
how would i go about doing this without delay? i've looked at the examples but they dont make sense to me...
arghhh i wish this made more sense!
I have the button connected to a 100 ohm resistor then to vcc the other end to ground.
Ok ill give it a shot thanks
ok after a bit of wrestling with it i finally got it working. now to order some rgb led's so i can program them to light up according to what state they are in so i know the delay that i will have.
...
hold on while writing this and looking up rgb led's i realized you need 3 free pins and im on an attiny85
and only have like 2 free pins so my idea is use a regular led and make it flash fast for a delay setting that is fast for example delay = 10 and slower as it goes to a full non flashing led at lets say delay = 5000
=]]]]]
super happy i have come this conclusion.
now to try to get it to work haha.
thank you everyone for pitching in!
Wesley5n1p35:
if (buttonPushCounter % 1 == 0) {
delayTime = 1000;
}
if (buttonPushCounter % 2 == 0) {
delayTime = 3000;
}
if (buttonPushCounter % 3 == 0) {
delayTime = 5000;
}
if (buttonPushCounter % 4 == 0) {
delayTime = 10;
}
it doesnt work though.
No kidding. What's the remainder when you divide 5 by 1? 0. When you divide 6 by 1? 0.
What you mean is:
if (buttonPushCounter % 4 == 0) {
delayTime = 1000;
}
if (buttonPushCounter % 4 == 1) {
delayTime = 3000;
}
if (buttonPushCounter % 4 == 2) {
delayTime = 5000;
}
if (buttonPushCounter % 4 == 3) {
delayTime = 10;
}
or better still:
switch(buttonPushCounter % 4) {
case 0: delayTime = 1000; break;
case 1: delayTime = 3000; break;
case 3: delayTime = 5000; break;
case 4: delayTime = 10; break;
}