Show Posts
Pages: 1 ... 20 21 [22] 23 24 ... 233
316  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 10, 2014, 12:08:12 pm
A quick note about my code above.  counter should be declared as an unsigned int otherwise the index to the array is not correct when the int goes negative.
317  Using Arduino / Programming Questions / Re: Why does this delay hang with certain values? on: March 10, 2014, 11:51:44 am
That cannot be the code that you are actually running as it does not compile.

digitaWrite in 2 places instead of digitalWrite and a semi-colon on the end of the for loop statement
318  Using Arduino / Programming Questions / Re: using millis to get round delays inside an if statement (xylophone servo) on: March 10, 2014, 11:43:21 am
This will never be true
Code:
  if (millis() - timestarted >= 800)
  {
    digitalWrite(led, LOW);
    timestarted = millis();
    Serial.println("shouldbeon2");
  }
because timeStarted gets reset to the current millis() value when
Code:
  if (millis() - timestarted >= 400)
is true.

There is no reason why you cannot have 2 variables for different start times or stick with one variable and flip the state of the LED each time the period ends.
319  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 10, 2014, 05:16:47 am
That depends what you mean by simple.  You can avoid checking which character is currently showing using an array, but you might not regard that as simple.
Code:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3

LiquidCrystal_I2C lcd(I2C_ADDR);  //NOTE library changed by BB to hard code pin numbers

unsigned long startedFlash;
unsigned long interval = 500;
char charsToShow[] = {'o', ' '};
int counter = 0;

void setup()
{
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  lcd.print("Hello World");
}

void loop()
{
  if (millis() - startedFlash > interval)
  {
    lcd.setCursor(4, 0);
    lcd.print(charsToShow[(counter++) % 2]);
    startedFlash = millis();
  }
}
Bear in mind that most of the code is to do with setting up the LCD.
320  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 10, 2014, 03:48:14 am
In my code example charToPrint was declared as a global char but you could declare it at the start of the loop() function as that is the only place that it is used.

I did not post all of the declaration and setup stuff because I have a different LCD and did not want to confuse things further but here is the whole program for my system
Code:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3

LiquidCrystal_I2C lcd(I2C_ADDR);  //NOTE library changed by BB to hard code pin numbers

unsigned long startedFlash;
unsigned long interval = 500;
char charToPrint = ' ';

void setup()
{
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  lcd.print("Hello World");
}

void loop()
{
  if (millis() - startedFlash > interval)
  {
    if (charToPrint == ' ')
    {
      charToPrint = 'o';
    }
    else
    {
      charToPrint = ' ';
    }
    startedFlash = millis();
    lcd.setCursor(4, 0);
    lcd.print(charToPrint);
  }
}
321  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 10, 2014, 03:29:17 am
So, what is it supposed to do and what does it really do ?
Where does getchar come into it as the variable does not appear in my program or yours.
322  Using Arduino / Programming Questions / Re: using millis to get round delays inside an if statement (xylophone servo) on: March 09, 2014, 05:20:27 pm
ok cheers not quite sure I understand though

Code:
start of loop
  if start signal received and nowPlaying is false
    set timeStarted = millis()
    set nowPlaying to true
  end if

is now playing a boolean value ?

I found this code online for controlling the blinks of an LED

Code:
void loop()
{
  currentTime = millis();
  if(currentTime >= (loopTime + 1000)){  
    digitalWrite(ledPin, !digitalRead(ledPin));   // toggles the LED on/off
    loopTime = currentTime;  // Updates loopTime
  }

which I though I could replicate for each note (inside an if statement) changing the +1000 to correspond to the times of the notes?
In my pseudo code nowPlaying is a boolean.  I put it in to prevent any problems if the start signal occurred again or persisted when the note was already playing.

There is no reason for interval in my code to be a fixed value.  You could have two arrays, one with the note to be played and the other the duration of the note.  Using a common index into the array would allow a note to be played for the appropriate duration.
323  Using Arduino / Programming Questions / Re: coding help on: March 09, 2014, 02:57:50 pm
If a test fails set a flag to indicate faulure.  Then check the flag before each test.  If it is set don't do the test.
324  Using Arduino / Project Guidance / Re: Detecting a Change In Sign on: March 09, 2014, 02:54:07 pm
Each time you read the value save it.  Next time you read it compare it with the previous value.

if previous value greater than zero and current value less than zero the reading has gone from positive to negative
if current value greater than zero and previous value less than zero the reading has gone from negative to positive

I strongly suspect that there is a smarter way to do this ....
325  Using Arduino / Programming Questions / Re: using millis to get round delays inside an if statement (xylophone servo) on: March 09, 2014, 02:24:47 pm
Don't bother setting millis() to zero, even if you can.  Just play the next note when enough time has elapsed
Code:
start of loop
  if start signal received and nowPlaying is false
    set timeStarted = millis()
    set nowPlaying to true
  end if
 
  if millis() - timeStarted > = interval
    play the next note
    set timeStarted = millis()
  end if
end of loop
326  Using Arduino / Programming Questions / Re: coding help on: March 09, 2014, 02:12:58 pm
What do you mean by "stop the program if a test fails" ?
Taking this test for example
Code:
  if(val1 == HIGH)
  {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    delay(2000);
  }
  else
  {
    digitalWrite(led2, HIGH);
    digitalWrite(led1, LOW);
    delay(2000);
  }
What constitutes a failure and what should happen ?
327  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 09, 2014, 09:59:37 am
I am not going to reply to this thread until your current code is posted properly.
328  Using Arduino / Programming Questions / Re: Arduino UNO sketch & push button on: March 09, 2014, 09:56:38 am
Some code to get you started
Code:
int state = 0;
const byte autoButtonPin = 2;
const byte manuButtonPin = 3;

void setup()
{
  pinMode(autoButtonPin, INPUT_PULLUP);
  pinMode(manuButtonPin, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop()

  switch (state)
  {
  case 0:
    if (digitalRead(autoButtonPin) == LOW)
    {
      Serial.println("Auto button pressed.  Waiting for manual button to be pressed");
      state = 1;
    }
    break;

  case 1:
    if (digitalRead(manuButtonPin) == LOW)
    {
      Serial.println("Manual button has been pressed");
      state = 0;
    }
    break;
  }
}
329  Using Arduino / Programming Questions / Re: arduino and ws2803 led driver on: March 09, 2014, 03:36:39 am
Can you please copy/paste your code from the IDE straight into code tags to avoid all the color crap ?
330  Using Arduino / Programming Questions / Re: lcd blink particular index variable on: March 09, 2014, 02:46:27 am
Try this.  Not a delay() in sight because I presume that the program will need to do other things and not be blocked.
Code:
  lcd.setCursor(0, 0);
  lcd.print("Hello World");
}

void loop()
{
  if (millis() - startedFlash > interval)
  {
    if (charToPrint == ' ')
    {
      charToPrint = 'o';
    }
    else
    {
      charToPrint = ' ';
    }
    startedFlash = millis();
    lcd.setCursor(4, 0);
    lcd.print(charToPrint);
  }
}
Obviously you need to set up the variables appropriately but this is the loop that does the time checking and flashes the character.  There is a neater way to do it that I can think of but this one is very obvious as to how it works.
Pages: 1 ... 20 21 [22] 23 24 ... 233