Hi, im quite new to Arduino and have just started making my own programs.
I have a slight problem. i am trying to build a simple 3 led, two buttons program, which when you press the first button, the first LED lights, with all the others flashing, when you press the second button, the second LED lights, with the others flashing, and when you press both, the third LED lights and the other flash.
so far this is all the code i have
//Simple 3 LED, 2 button program
int but1pin = 5 //button one, attachted to pin 5
int but2pin = 7 //button two, attachted to pin 7
int led1pin = 13 //first led, large blue led, attachted to pin 13
int led2pin = 11 //second led,small red led, attachted to pin 11
int led3pin = 9 //third led,small green led, attachted to pin 9
void setup() {
pinMode(but1pin = INPUT)//sets button 1 as an input
pinMode(but2pin = INPUT)//sets button 2 as an input
pinMode(led1pin = OUTPUT)// sets led 1 as an output
pinMode(led2pin = OUTPUT)// sets led 2 as an output
pinMode(led3pin = OUTPUT)// sets led 3 as an output
}
void loop(){
val = digitalRead(
Im not really sure were to go from here. Any help will be appreciated
pinMode(but1pin = INPUT)//sets button 1 as an input
This will compile, and be easier to read, like this:
pinMode(but1pin, INPUT); //sets button 1 as an input
Now, you need to add code to read whether a switch has been pressed. The switch is a digital device (on or off) that you want to Read. Perhaps using digitalRead.
The digitalRead function will return HIGH or LOW, depending on whether the switch is pressed, or not. Which it returns when the switch is pressed depends on how the switch is wired. Experiment to find out.
The LED is another digital device (on or off) that you Write to. Try using digitalWrite. To make the LED flash, you turn it on, wait, and turn it off.
You can wait in two ways. One is to use the delay function. That's easy, but NOTHING happens while you wait. The other is to use the millis() function. Look at the BlinkWithoutDelay example to see how to use it.
i know, just relised that, basically it came up with: error:expected unqualified-id before numeric constant
the code is now :
//Simple 3 LED, 2 button program
int but1pin = 5 //button one, attachted to pin 5
int but2pin = 7 //button two, attachted to pin 7
int led1pin = 13 //first led, large blue led, attachted to pin 13
int led2pin = 11 //second led,small red led, attachted to pin 11
int led3pin = 9 //third led,small green led, attachted to pin 9
void setup() {
pinMode(but1pin = INPUT)//sets button 1 as an input
pinMode(but2pin = INPUT)//sets button 2 as an input
pinMode(led1pin = OUTPUT)// sets led 1 as an output
pinMode(led2pin = OUTPUT)// sets led 2 as an output
pinMode(led3pin = OUTPUT)// sets led 3 as an output
}
In this version, the loop function went away. I'm not sure why.
val = digitalRead(but1pin == HIGH)
val is not declared. This argument to the digitalRead function will be true or false (1 or 0) depending on whether but1pin currently contains HIGH. Since but1pin was assigned a value of 5, and that is not HIGH, but1pin == HIGH will evaluate to false (0), so this code will try to do a digitalRead on pin 0.
You want something like this:
int val = digitalRead(but1pin);
if(val == HIGH)
{
// Do whatever needs to be done when the button is pressed
}
millis is a function. It needs to be called like this:
Here's some quick advice. Use "#defines" for CONSTANTS, i.e. numbers that are used in your code that don't ever need to change. That way, the compiler will insert the numbers during assembly, and won't reserve RAM locations in your microcontroller for those numbers that never change.
#define but1pin 5 //button one, attachted to pin 5
#define but2pin 7 //button two, attachted to pin 7
#define led1pin 13 //first led, large blue led, attachted to pin 13
#define led2pin 11 //second led,small red led, attachted to pin 11
#define led3pin 9 //third led,small green led, attachted to pin 9
void setup() {
pinMode(but1pin, INPUT) //sets button 1 as an input
pinMode(but2pin, INPUT); //sets button 2 as an input
pinMode(led1pin, OUTPUT); // sets led 1 as an output
pinMode(led2pin, OUTPUT); // sets led 2 as an output
pinMode(led3pin, OUTPUT); // sets led 3 as an output
}
Some things to note:
#define doesn't need ';' or '=' #define but1pin 5
int needs '=' AND ';' int but1pin = 5;
pinMode() uses two arguments separated by a ',' with a ';' at the end pinMode(PIN_NUMBER, INPUT or OUTPUT);
Uppercase your constants, so you don't confuse them for variables (especially helpful as you code longer programs).
ie:
#define BUTTON_1 5 // button one, attached to pin 5
Also, while I didn't do it in the example above, make liberal use of spacing and such in your formatting of code (and comments) so as to make the code easier to read and understand. Dense code is difficult to read; make it less dense, and continue to make liberal use of comments.
This advice comes from someone with over 25 years of coding experience, in every language from Logo to C++, and tons of stuff in-between (including Apple 2e hand assembly via hex in the monitor program, and 68000 assembly on an Amiga); trust me, your eyes and your mind (and your hair) will thank me later!
Good point cr0sh! I was thinking it, but didn't want to impose too much at once Also didn't want to be a hypocrite because I don't always follow this rule, but should!
I was about to post an alternative using const, but then it suddenly hit me, does const reserve RAM memory for its values? In which case I'll begin using #define too.
const int BUTTON_1 = 5[glow];[/glow] // button one, attached to pin 5
I'm guilty of not always uppercasing constants myself. But I probably should.
I would say the const int is like an application coder's way of implementing it on a microcontroller, but #define is an embedded coder's way of doing it. I'm sure there's crossover there, but that my viewpoint.
Shaun - did you succeded with your program. If not you can look at this code:
// Two button blink
// Buttons are attached to pin 5 nd 7. Both pins are protected with
// internal pull-up resistors. Pins are HIGH when unpressed and LOW
// when pressed. Leds are attached to pin 13, 11 and 9.
// 3 220 ohm resistor is in serie on the pins.
// If button 1 are pressed led 1 is on and the two others blinks.
// If button 2 are pressed led 2 is on and the two others blinks.
// If button 1+2 are pressed led 3 is on and the two others blinks.
// -Fletcher
const int but1 = 5;
const int but2 = 7;
const int led1 = 13;
const int led2 = 11;
const int led3 = 9;
const int blinkdelay = 50; // Low number = high blink rate
void setup(){
pinMode(but1, INPUT);
digitalWrite(but1, HIGH); // Enable pull-up resistor
pinMode(but2, INPUT);
digitalWrite(but2, HIGH); // Enable pull-up resistor
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// *** Main program ***
void loop(){
if ((digitalRead(but1) == HIGH) and (digitalRead(but2) == HIGH)) turnoff(led1, led2, led3);
if ((digitalRead(but1) == LOW) and (digitalRead(but2) == HIGH)) turnon (led1, led2, led3);
if ((digitalRead(but1) == HIGH) and (digitalRead(but2) == LOW)) turnon (led2, led1, led3);
if ((digitalRead(but1) == LOW) and (digitalRead(but2) == LOW)) turnon (led3, led2, led1);
}
// *** Main program end ***
void turnoff (const int a, const int b, const int c){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
}
void turnon (const int a, const int b, const int c){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
delay(blinkdelay);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
delay(blinkdelay);
}