NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Basic Electronics » Pushbutton on off code

February 23, 2011
by amartinez
amartinez's Avatar

I have just completed the traffic light project. Very cool. I was interested in the piece of code scanning for the button to be pushed. Here I see in usual cryptic C language that when the button is pushed, the variable "walk" is set to 1

// loop forever! while(1) { // check the walk button if((PINC & (1<<PC5)) == 0) walk = 1;

I am not too "C" saavy, but, following the program further down I see a conditional statement as such

case STATE_2Y: if(walk) { state = STATE_WALK; lcd_write_string(PSTR("Cars stopped. WALK")); set_lights(0x11); timeout = 500; walk = 0;

Which I understand writes to the LCD then resets the variable "walk" to = 0.

I am trying to make the pushbutton switch turn something like an LED on when I push it, then off when I push it again. A VOM shows that the pins put out 2.7 volts which should suffice to turn on an LED.

I would need to keep the variable "walk" to 1 until I press the button again.

I am not very good with conditional statements in C but if you could refrain from laughing, I'm going to show some hybrid code in BASIC to help me formulate a clearer question. (Yes, I know, I'm stuck in the 1980s with my Commodore 64, I'm trying )

Ok, here is the code

10 wait = 0 (scan for push of button before you go on)

20 wait = wait +1 30 if wait = 2 then wait = 0

40 if wait = 1 then (turn the LED in PB1 on) 50 if wait = 0 then (turn the LED in PB1 off)

60 GOTO 10

Here is my question. If anyone can be so kind as to show me where I can find similar code to in C that will do the job for me.

Everything in the kit works great, all of the projects are no problem setting up. Humberto is correct, not every project needs a schematic, it can be figured out from the code.

Where I get stuck is the C code.

Thank you for your help and not laughing too loud

Al

February 23, 2011
by Ralphxyz
Ralphxyz's Avatar

I cleaned your post up for you so that others can read it.

I have just completed the traffic light project. Very cool. I was interested in the piece of code scanning for the button to be pushed. Here I see in usual cryptic C language that when the button is pushed, the variable "walk" is set to 1

// loop forever! 
while(1) { // check the walk button 
if((PINC & (1<<PC5)) == 0) walk = 1;

I am not too "C" saavy, but, following the program further down I see a conditional statement as such

case STATE_2Y: 
    if(walk) 
    { 
        state = STATE_WALK; 
        lcd_write_string(PSTR("Cars stopped. WALK")); 
        set_lights(0x11); 
        timeout = 500;    
        walk = 0;

Which I understand writes to the LCD then resets the variable "walk" to = 0.

I am trying to make the pushbutton switch turn something like an LED on when I push it, then off when I push it again. A VOM shows that the pins put out 2.7 volts which should suffice to turn on an LED.

I would need to keep the variable "walk" to 1 until I press the button again.

I am not very good with conditional statements in C but if you could refrain from laughing, I'm going to show some hybrid code in BASIC to help me formulate a clearer question. (Yes, I know, I'm stuck in the 1980s with my Commodore 64, I'm trying )

Ok, here is the code

10 wait = 0 (scan for push of button before you go on)

20 wait = wait +1 30 if wait = 2 then wait = 0

40 if wait = 1 then (turn the LED in PB1 on)

50 if wait = 0 then (turn the LED in PB1 off)

60 GOTO 10

Here is my question. If anyone can be so kind as to show me where I can find similar code to in C that will do the job for me.

Everything in the kit works great, all of the projects are no problem setting up. Humberto is correct, not every project needs a schematic, it can be figured out from the code.

Where I get stuck is the C code.

Thank you for your help and not laughing too loud

Al

February 26, 2011
by amartinez
amartinez's Avatar

Got it. C tutorials on the net really help.

Here's my code to count up on an LCD screen. Conditional statements are not hard if you have the concept down from another programming language.

define F_CPU 14745600

include <stdio.h>

include <avr/io.h>

include <avr/interrupt.h>

include <avr/pgmspace.h>

include <inttypes.h>

include "../libnerdkits/delay.h"

include "../libnerdkits/lcd.h"

int main() {

// start up the LCD lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_clear_and_home();

// ummm... I added this in?

int al_count;
al_count=0;

while(al_count<5){

al_count=al_count+1;
delay_ms(1000);

// print message to screen // 20 columns wide: // 01234567890123456789 lcd_line_one(); // lcd_write_string(PSTR(" I am XZHORKON!!! ")); // lcd_line_two();

  lcd_write_string(PSTR("Al Counting>>: "));
  lcd_write_int16(al_count);

}

delay_ms(2000);

lcd_clear_and_home();

lcd_write_string(PSTR("THAT'S ALL FOLKS"));

return 0; }

Post a Reply

Please log in to post a reply.

Did you know that you can build an analog amplifier with one transistor and a few resistors? Learn more...