Newbie trying to write code for first time

Hi, all,
I am a total newbie and am trying to write my first code. I know absolutely nothing about programming but want to learn some simple stuff. Current project I am attempting is alternating LEDs with a pushbutton switch connected to pin 2. Yellow LED to pin 3 and red LED to pin 4. I can paste the schematic if needed. Essentially, want to check the state of the pushbutton - if high, light yellow, if low, light red.

I know I can paste the code here in the forum but can I cut and paste the errors I am getting? Or would just posting the code be enough for someone to spot the errors?

Are there any references for the ultra beginner?

I tried copying some other examples but apparently missed something along the way :~

TIA

Larry

[/quote]

Or would just posting the code be enough for someone to spot the errors?

It's much better than us trying to guess what your code is.
Remember select the code once you paste it and hit the #icon before posting.
I hope you have resistors in line with those LEDs.

Thanks, GM. Did not really want you to guess - did not know if I could post the error messages or just had to post the code and let you guys look for the errors. Yes, I do have resistors in line with the LEDs. Thanks for the tip about the #icon as well.

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color] (){
  [color=#7E7E7E]// initialize pin 2 as input[/color]
  (2, [color=#006699]INPUT[/color]);
  [color=#7E7E7E]//initialize pin 3 as output Y[/color]
  (3, [color=#006699]OUTPUT[/color]);
  [color=#7E7E7E]//initialize pin 4 as output R[/color]
  (4, [color=#006699]OUTPUT[/color]);
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color] (){
  [color=#7E7E7E]//read state of pushbutton value[/color]
  button state = digital [color=#CC6600]read[/color] (button pin);
  [color=#CC6600]if[/color] (button state ==[color=#006699]HIGH[/color]){
    digital [color=#CC6600]write[/color] (2, [color=#006699]HIGH[/color]);
  }
      [color=#CC6600]else[/color] {
      digital [color=#CC6600]write[/color] (2, [color=#006699]LOW[/color]);}
      
      [color=#7E7E7E]//if button is high, light Yellow LED[/color]
      digital [color=#CC6600]write[/color] (3, [color=#006699]HIGH[/color]);
}    [color=#CC6600]else[/color] {
    digital [color=#CC6600]write[/color] (4, [color=#006699]HIGH[/color]);

}

[/quote]

OK try again,
just select all the code in the arduino IDE, copy it and past it into your reply.
Do not use any format for net or other options.
That code you posted is full of HTML and it takes an age removing it.

void setup (){
// initialize pin 2 as input
(2, INPUT);
//initialize pin 3 as output Y
(3, OUTPUT);
//initialize pin 4 as output R
(4, OUTPUT);
}
void loop (){
//read state of pushbutton value
button state = digital read (button pin);
if (button state ==HIGH){
digital write (2, HIGH);
}
else {
digital write (2, LOW);}

//if button is high, light Yellow LED
digital write (3, HIGH);
} else {
digital write (4, HIGH);

You forgot the #icon.
You did not post all the code. Missing }s
You can't have variable names with a space.
You need to declare variables before you use them.
You put spaces between key function names.
You are trying to use two buttons but have only defined one.
This will compile:-

void setup (){
  // initialize pin 2 as input
  pinMode(2, INPUT);
  //initialize pin 3 as output Y
  pinMode(3, OUTPUT);
  //initialize pin 4 as output R
  pinMode(4, OUTPUT);
}
int button_state;
int button_pin = 5; // or what ever pin your button you use

void loop (){
  //read state of pushbutton value
  button_state = digitalRead(button_pin);
  if (button_state ==HIGH){
    digitalWrite (3, HIGH);
    digitalWrite (4, LOW);
  }
      else {
      digitalWrite (4, HIGH); 
      digitalWrite (3, LOW);
  }
    
}

Edit. you were also writing to an input pin.

Thanks, Mike. Dinner time over here on the other side of the pond. Will try your sketch in the AM. Also want to compare line by line to see where I went wrong.

Mike, I cut and pasted the code you wrote and when compiling got this error "function definition does not declare parameters". Once again, I do not know enough to correct that.

Also, just using one button to toggle between the two LEDs - you mentioned two.

Is there a resource that explains "compiling errors" as well as where { }, (), ; etc. should be used? I have the "language" from the Arduino home page but not much else on writing code.

Thanks again - appreciate your help

Mike, I cut and pasted the code you wrote and when compiling got this error "function definition does not declare parameters"

The code in reply #5 compiles perfectly. (at least at 0022 it does)
However, pin 5 is only an input by default.
I'd go for:

const int button_pin = 5; // or what ever pin your button you use

void setup ()
{
  pinMode(button_pin, INPUT);
  //initialize pin 3 as output Y
  pinMode(3, OUTPUT);
  //initialize pin 4 as output R
  pinMode(4, OUTPUT);
}

void loop ()
{
  //read state of pushbutton value
  int button_state = digitalRead(button_pin);
  if (button_state == HIGH) {
    digitalWrite (3, HIGH);
    digitalWrite (4, LOW);
  } else {
      digitalWrite (4, HIGH); 
      digitalWrite (3, LOW);
  } 
}

AWOL,

Any idea why if I cut and pasted, it would not compile and came up with the error? I must be doing something wrong somewhere if it compiled perfectly for you??

Cannot cut and paste the schematic - here is the link to the project:

http://itp.nyu.edu/physcomp/Labs/DigitalInOut

Thanks

Try posting the code that gave you the compilation error from the IDE window here.
Don't forget to use the # icon on the editor's toolbar.

Right out of the IDE:

Code:
void setup (){
  // initialize pin 2 as input
  pinMode(2, INPUT);
  //initialize pin 3 as output Y
  pinMode(3, OUTPUT);
  //initialize pin 4 as output R
  pinMode(4, OUTPUT);
}
int button_state;
int button_pin = 5; // or what ever pin your button you use

void loop (){
  //read state of pushbutton value
  button_state = digitalRead(button_pin);
  if (button_state ==HIGH){
    digitalWrite (3, HIGH);
    digitalWrite (4, LOW);
  }
      else {
      digitalWrite (4, HIGH); 
      digitalWrite (3, LOW);
  }
    
}

Well, if you get rid of the "Code:" label (where did that come from?), that compiles perfectly too.

I wondered if that was the problem :slight_smile: I included it from here with the cut and paste :~.

Compiled perfectly but alas, the project is not working - only the red LED comes on and the pushbutton has no effect - will check the wiring but pretty sure it is okay..................

Thanks

Comment out the digitalRead and replace it with button_state = HIGH;
That should tell you if the yellow LED is wired correctly, then you're left with the switch as the source of your problem.

and the pushbutton has no effect

What pin do you have it wired to. In your original code this was pin 5, if it is pin 2 then change:-

int button_pin = 5; // or what ever pin your button you use

to

int button_pin = 2; // or what ever pin your button you use

You will have to learn to read a program.

Thanks, Mike - changing to pin 2 as you suggested has it working fine :slight_smile: Have no idea where the 5 came from since I was only using 2,3 and 4. Maybe a typo??

Are there any resources out there on how to learn to read/write code for the ultra beginner? Definitions of Digital Read, Digital Write, etc.? I can glean a bit from examples but it would be nice to have a reference.

Thanks again - really appreciate the help. I did compare your code and mine and saw what I left out so am learning a little. Looks like learning code and programming might not be an overnight affair..................................but I did get the "setup" correct :slight_smile:

but I did get the "setup" correct

well, sort of.
Your setup whilst correct, would mean that you would be prone to the sort of bug you just found and/or would be difficult to maintain if you changed pin assignments.
If you give a symbolic name like "pushButtonPin" or "redLEDpin" to the pin number, and use that instead of the magic number "5" or "2" or whatever, then your code remains consistent throughout.

but I did get the "setup" correct

Well no actually you had:-

void setup (){
  // initialize pin 2 as input
  (2, INPUT);
  //initialize pin 3 as output Y
  (3, OUTPUT);
  //initialize pin 4 as output R
  (4, OUTPUT);
}

Where as it should have been:-

void setup (){
  // initialize pin 2 as input
  pinMode(2, INPUT);
  //initialize pin 3 as output Y
  pinMode(3, OUTPUT);
  //initialize pin 4 as output R
  pinMode(4, OUTPUT);
}

Spot the difference.

Are there any resources out there on how to learn to read/write code for the ultra beginner? Definitions of Digital Read, Digital Write, etc.?

Not out there but actually in there. If you pull down the help menu and select reference you get a page of commands and clickable links to an explanation of how to use them. You don't even have to be on line it is all built into the IDE.

difference being you included pinMode.

Thanks for the tip about the help menu in the IDE. I also discovered Arduino Learning, Foundations, Variables and the associated subjects so that will help a lot.

Are there keyboard shortcuts for the different variables or commands??

Right now just sitting here pushing the button and smiling :slight_smile: :slight_smile: :slight_smile: