2 Replies - 531 Views - Last Post: 01 June 2013 - 10:23 AM Rate Topic: -----

#1 StewartC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-May 13

Arduino coding

Posted 30 May 2013 - 07:49 PM

Hi everyone, I just started tinkering with the Arduino and I only just started learning about coding fundamentals. That being said, I need some help with some code I tried to write. I couldn't figure out my code's problems from google searches or searches from this subreddit alone.
I want to code a digital watch that uses 5 LEDs and resets every 5 minutes, functioning as sort of a short-term activity watch (mostly to get my feet wet with Arduino projects). The way I imagined it being set up is: 4 LEDs blink 15 times in 15 seconds each, in succession (LED 1 blinks 15 times, then LED 2 blinks 15 times, and so on). Every time a minute passes, the second value should reset to 0 and a fifth LED changes its state/blinking pattern to show what minute it is. When the fifth minute passes, the minute count should reset.
This is the code I tried to write:
int minled = 12;
int second = 0;
int minute = 1;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(minled, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(second); //second can be seen
delay(500);
//minute 1
do {
digitalWrite(minled, LOW);
}
while (minute = 1);
//minute 2
do {
digitalWrite(minled, HIGH);

delay(100);

digitalWrite(minled, LOW);

delay(900);
}
while (minute = 2);
//minute 3
do {
digitalWrite(minled, HIGH);

delay(100);

digitalWrite(minled, LOW);

delay(400);
}
while (minute = 3);
//minute 4
do {
digitalWrite(minled, HIGH);

delay(1000);

digitalWrite(minled, LOW);

delay(1000);
}
while (minute = 4);
//minute 5
do {
digitalWrite(minled, HIGH);
}
while (minute = 5);
//seconds
while(second <= 15) {
//1-15 sec led
digitalWrite(8, HIGH); //blinks every second
delay(100);
digitalWrite(8, LOW);
delay(900);
second++;
}
while(second <= 30) {
//16-30sec led
digitalWrite(9, HIGH); //blinks every second
delay(100);
digitalWrite(9, LOW);
delay(900);
second++;
}
while(second <= 45) {
//31-45sec led
digitalWrite(10, HIGH); //blinks every second
delay(100);
digitalWrite(10, LOW);
delay(900);
second++;
}
while(second <=60) {
//46-60sec led
digitalWrite(11, HIGH); //blinks every second
delay(100);
digitalWrite(11, LOW);
delay(900);
second++;
}
//resetting values
if(second = 60) { //when at 60 seconds, minute goes up by 1 and second = 0
minute++;

second = 0;
}
if(minute = 6) { //when at 6 minutes, reset to minute 1 and second 0
minute = 1;

second = 0;
}
}

I tried to hook up just the first LED (the 1-15 seconds LED) with this sketch uploaded to the Arduino and it wouldn't blink or do anything at all. Then I tried uploading a sketch saying only to turn that LED on to check that my connections in the circuit were ok and then the LED turned on. So I figured it's a problem with the code.
If anyone could skim through this and point out the errors I made in the code so that I can get this thing working, I'd really appreciate it! (:
edit: added spacing in the code to improve readability

This post has been edited by modi123_1: 30 May 2013 - 07:53 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Arduino coding

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Arduino coding

Posted 30 May 2013 - 07:58 PM

1) indent your code correctly nobody will waste his time to read that
2) what is the delay(), digitalWrite() and pinMode() methods ?
3) what is the Serial class ?
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

Re: Arduino coding

Posted 01 June 2013 - 10:23 AM

First off, the delay() function deals with milliseconds and not seconds, so by saying minute = whatev, you're wrong. In addition, a comparison is made with ==, not = in the while loops. AND you don't need any while loops. use a switch statement on the minute variable to determine what minute (1-12) you are in. THEN you can handle the case by calling a method that turns off all LEDs and then you can turn on the one you want. Maybe something like this:

switch(minute):
    turnOffAllLEDs();
    case 1:
        digitalWrite(1, HIGH);
        break;
    case 2:
        digitalWrite(2, HIGH);
        break;
// and so on



Finally, make sure your minute counter is actually accumulating the values (check out the millis function: http://arduino.cc/en/Reference/millis)

Also, Moved to Other Languages. This is not Java.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1