Go Down

Topic: this is a good one... mind funk (Read 936 times) previous topic - next topic

Ste Hughes

basically I am moding my TV remote so that I can flick through channels quicker. ignore the project aim the mind messing comes later.

I plan to use a ATTiny 85 because of its size. I have 5 I/O's but there other stuff I want to add so be resourceful

Basically I have 1 point where I can solder too. My idea is to make 1 output pulse high and low when the button is low (when its being pressed) Something like this

If button is low {
output high
delay
output low
delay}

Excuse the code just keeping it super simple.

but How do I do this? because all i see happening is it gets stuck in a loop and just carrys on pulsing for ever...

Am I wrong or what ever?

How would you go about this... There must be a solution.

Cheers boys and girls

Grumpy_Mike

Quote
because all i see happening is it gets stuck in a loop and just carrys on pulsing for ever.

You need to post the actual code (using the # icon) so we can see where you messed up.
However look at the 'while' statement for a better approach to this problem.

Ste Hughes

Code: [Select]
const int pulse = 2;

void setup() {   
pinMode(pulse, OUTPUT);

}

void loop() {
 
  int buttonstate = digitalRead (pulse);
  if (buttonstate == LOW) {
    digitalWrite(pulse, HIGH); 
    delay(75);             
    digitalWrite(pulse, LOW);   
    delay(25);         
 
}}


Here it is Mike.

I am god awful at code but the way I see it if i set the pulse to pulse... it will pulse and just loop?

Grumpy_Mike

You can't use a pin as an input and an output at the same time. You have to have one pin for the input button and another for the output button. Then read the input and pulse the output for as long as the input is pushed.

Ste Hughes

Code: [Select]
const int Pulse = 2;
const int button = 4;



void setup() {   
pinMode(Pulse, OUTPUT);
pinMode(button, INPUT); 

}

void loop() {

int buttonstate = digitalRead (button); 

while(buttonstate == LOW){
    digitalWrite(Pulse, HIGH); 
    delay(75);             
    digitalWrite(Pulse, LOW);   
    delay(25);
}}


Ok thats what I have now but it just ignores the button press and is 5v all the time even when i press the button... If i remove the ATtiny it works fine.

Grumpy_Mike

You need to read the button state inside the while loop so that when it changes you know about it.

Ste Hughes

Thanks Mike,

Code: [Select]
const int Pulse = 2;
const int button = 4;



void setup() {   
pinMode(Pulse, OUTPUT);
pinMode(button, INPUT); 

}

void loop() {

  while(digitalRead(button)==LOW){
    digitalWrite(Pulse, HIGH); 
    delay(75);             
    digitalWrite(Pulse, LOW);   
    delay(25);
}}


that is what I have now :)

But it just keeps pulsing no matter what now...

Thanks for your help so far it is very much appreciated. I don't want it doing for me but I can't get my head around it.

CrossRoads

How is the button wired? Do you have an external pullup to 5V so it can read as  High when not pressed?
Or turn on the internal pullup:

pinMode(button, INPUT);
// add this next line to your code
digitalWrite(button, HIGH);  // enables internal pullup
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Ste Hughes

Thanks. On my breadboard it works perfectly but on the remote it doesn't. Again it just keeps pulsing.

The schematic is simple the 1 solder point I have available on the PCB. It has 2 wires coming off, one to each pin.

I thought it was an electronics problem rather than programming. If I have 1 pin pulsing low and high and one looking for the low (when the button is pressed) surely no matter what it will just go into a dirty loop?

Grumpy_Mike

#9
May 11, 2011, 11:18 pm Last Edit: May 11, 2011, 11:21 pm by Grumpy_Mike Reason: 1
When connecting two devices there must be a common ground, you can't just connect the signals.

Also you can not have a wire acting as an input and an output at the same time. If you only have one solder point you only have one signal, it can't be a control input and an output.

Go Up