Pages: [1]   Go Down
Author Topic: starting out with arduino  (Read 273 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I got a arduino for christmas yesterday and have started using it, i feel i have a understanding of it but i have come across a problem i cant seem to understand

i will first post my sketch then explain my problem
Code:
const int button = 7;
const int led = 2;

int ledState = 1;
int ledSwitch = -1;
int buttonState = 0;

void setup(){
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, ledState);
  Serial.begin(9600);
}

void loop(){
  buttonState = digitalRead(button);
  Serial.println(buttonState);
  if (buttonState == 1){
    for (int i = 0; i == 3000; i++){
      delay(10);
      Serial.println('test');
      if (digitalRead(button)==0) {
        ledState += ledSwitch;
        ledSwitch *= -1;
        digitalWrite(led, ledState);
      }
    }
  }
  delay(100);
}

i believe the problem is somewhere in my sketch not my wiring as the button and the led works perfectly fine

right my problem is, when i press the button i can tell the button goes high, as the serial monitor is filled with 1's when i press the button. It should then go into the for loop, however it never does, i can tell because the serial monitor never says "test" which it should do at least once
« Last Edit: December 26, 2012, 03:51:16 pm by Shuttleu » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int i = 0; i == 3000; i++)

The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

Doesn't sound right does it? The for loop will continue as long as the second statement in the for loop is true, so after setting i to 0, checking if it's 3000 will fail, and the loop will immediately exit.
« Last Edit: December 26, 2012, 03:55:34 pm by Arrch » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int i = 0; i == 3000; i++)

The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

Doesn't sound right does it?

i see, i havent done programming for a very long time, my mistake

it should be <= not ==

thank you!
Logged

Canberra Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Shuttleu,
   I am far from being proficient with coding  smiley-cool but just a few things I might suggest are -

   1. where you wish to print test in the serial monitor, test should be in double quotation marks i.e "test". I think that numbers
       use the single quotation marks.
   2. It is best to place a 1000ms delay after any println statement so you actually have time to read it before it scrolls off the
      serial monitor.
   Have fun with your Arduino.
Logged


Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int i = 0; i == 3000; i++)

The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

Doesn't sound right does it?

i see, i havent done programming for a very long time, my mistake

it should be <= not ==

thank you!

Note that that will run 3001 times
Logged

Pages: [1]   Go Up
Jump to: