Show Posts
Pages: 1 2 [3]
31  Using Arduino / General Electronics / L298N Problem with photo resistors on: March 31, 2012, 02:09:47 am
So, I am having a problem, hopefully somebody here has encountered it and has a solution. I am building a Photovore, and when I have my photo resistors hooked up directly to the Arduino, my light levels are correct (I.E. 920 directly under a flashlight). However in the exect same configuration under the exact same light source running the sensor on any analog pin on the motor shield, i get a reading like 28 or 30. I am poering directly from the USB at this point and this is with no motors running. I tried hooking up an additional power source (9V battery) via the DC jack on the Arduino, and I am get the same results (getting the results via serial print). Anybody know why this make be? I'm using a 10K ohm resistor ground and 5v out to the breadboard.

Here is my code

Code:
const int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1

int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON

long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
{
  Serial.begin(9600);  // open the serial port
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  pinMode(button, INPUT); // button is an input
}

void loop()
{
  val = digitalRead(button); // read input value and store it

  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
  }
  old_val = val; // val is now old, store it
  // check if button is pressed (HIGH)
  // and chage the state
  if (state == 1) {
    rVal = analogRead(righteye); // Read value from right eye sensor
    lVal = analogRead(lefteye); // Read value from left eye sensor
    analogWrite(pwm_a, rVal/4);  //set both motors to run at one quarter of light level
    analogWrite(pwm_b, lVal/4);
  }
  else {
    analogWrite(pwm_a, 0);  //set both motors to run at one quarter of light level
    analogWrite(pwm_b, 0);
  }
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Serial.println(rVal);
    Serial.println(lVal);
  }
}

Every once in a while with the motor shield (L928N) i get a value like 331 or so. This is proving to be a major stumbling point on the road to a working photovore.
32  Community / Exhibition / Gallery / My First Arduino Robot, A Photovore on: March 31, 2012, 12:07:51 am
Arduino Uno, L298N Motor Shield, 2 Photo Resistors, Push Button, Two Small DC Motor (Out of a broken helicopter not sure of the voltage or draw, but pretty small).

Code:
const int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1

int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON

void setup()
{
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  pinMode(button, INPUT); // button is an input
}

void loop()
{
  val = digitalRead(button); // read input value and store it

  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW)){
  state = 1 - state;
  }
  old_val = val; // val is now old, store it
  // check if button is pressed (HIGH)
  // and chage the state
  if (state == 1) {
  rVal = analogRead(righteye); // Read value from right eye sensor
  lVal = analogRead(lefteye); // Read value from left eye sensor
  analogWrite(pwm_a, rVal/4);  //set both motors to run at one quarter of light level
  analogWrite(pwm_b, lVal/4);
  } else {
    analogWrite(pwm_a, 0);  //set both motors to OFF
    analogWrite(pwm_b, 0);
  }
}

no body yet
33  Using Arduino / Motors, Mechanics, and Power / Re: L298N HELP!!?? on: March 30, 2012, 07:30:47 pm
Code:
void loop()
{
analogWrite(pwm_a, 255);  //set both motors to run at 100% duty cycle (fast)
}

haha the problem has that I ripped this motor out of a broken nano helicopter and the prong was bent, had to move it a bit now it works
34  Using Arduino / Motors, Mechanics, and Power / Re: L298N HELP!!?? on: March 30, 2012, 07:25:53 pm
Unfortunately there is no basic guide to this chip that I have boon able to find, hell if I had the knowledge I would write one. Unfortunately I am a newbie in the world of arduino, this is only my second day working with the board, and first with the L298N.
35  Using Arduino / Motors, Mechanics, and Power / L298N HELP!!?? on: March 30, 2012, 07:24:31 pm
So, I completed the getting started with Arduino guide and exercises. I proceeded to buy the L298N motor shield and two small dc motors (not steppers) and two Photo resistors. I am trying to make a basic photovore that will determine engine speed based on light level for each side and thereby steer. Fairly straightforward or so one would assume. I am going to be hooking up the motors to the a and b motor drivers on the L298N. I am currently trying to figure out how to control speed. I am using this code at present, which I found on some site:

Code:
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
int pwm_b = 9;  //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
int dir_a = 2;  //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
int dir_b = 8;  //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)

void setup()
{
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);

  analogWrite(pwm_a, 100);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, 100);

}

void loop()
{
  digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
  digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

  delay(1000);

  analogWrite(pwm_a, 255);  //set both motors to run at 100% duty cycle (fast)
  analogWrite(pwm_b, 255);

  delay(1000);

  digitalWrite(dir_a, HIGH);  //Reverse motor direction, 1 high, 2 low
  digitalWrite(dir_b, HIGH);  //Reverse motor direction, 3 low, 4 high

  delay(1000);

  analogWrite(pwm_a, 100);  //set both motors to run at (100/255 = 39)% duty cycle
  analogWrite(pwm_b, 100);

  delay(1000);
}

at this point I only have one motor hooked up and 9v batter going to the Uno board. The motor is hooked up to A, and I can hear it engage and the LED is lighting, however the motor is not rotating. I beesech one kind soul to hook me up with a working code snippet to drive the motor, and I can figure out the rest from there. Much thanks.
36  Using Arduino / Programming Questions / Re: Just Finished Getting Started w/ Arduino First Program, Question! on: March 30, 2012, 03:14:22 pm
My Idea was to build a small biped that can detect a soccer ball and kick it back to you, but I think thats beyond my skill right now. What I'm asking is what might be a good beginner/intermediate project. I bought the Arduino, because I am a web designer, however thought it would be cool to get involved in the world of physical programming. I was also considering running a chatbot on the biped so that it could have a conversation.. I have pretty much nixed that because there are no voice recognition shilds that can interperet the entire english language, the speech it not the problem there. I'm looking for a good suggestion that would help me continue to build my skills at programming and utilizing the Arduino.
37  Using Arduino / Programming Questions / Re: Just Finished Getting Started w/ Arduino First Program, Question! on: March 30, 2012, 01:32:35 am
Finished the whole book in one day, with the kit from Maker Shed. Any suggestions for a next project?
38  Using Arduino / Programming Questions / Re: Just Finished Getting Started w/ Arduino First Program, Question! on: March 30, 2012, 01:19:59 am
Solved, lol

Code:
#define SENSOR 0   
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12

int val = 0; // variable to store the value coming from the sensor

int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;

byte r = 0;
byte g = 0;
byte b = 0;

void setup() {
  Serial.begin(9600);  // open the serial port
  pinMode(BUTTON, INPUT);
}

void loop() {
  val = analogRead(SENSOR); // read the value from the sensor
  Serial.println(val);      // print the value to
                            // the serial port

if (Serial.available() > 0) {

    // read the incoming byte:
    inByte = Serial.read();

    // If the marker's found, next 6 characters are the colour
    if (inByte == '#') {

      while (pointer < 6) { // accumulate 6 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }

      // now we have the 3 numbers stored as hex numbers
      // we need to decode them into 3 bytes r, g and b
      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
      pointer = 0; // reset the pointer so we can reuse the buffer
     
    }
  }   

  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn; // val is now old, let's store it

  if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
 
    analogWrite(R_LED, r);  // turn the leds on
    analogWrite(G_LED, g);  // at the colour
    analogWrite(B_LED, b);  // sent by the computer
  }
  while ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
    analogWrite(R_LED, 0);  // turn the leds off
    analogWrite(G_LED, 0);
    analogWrite(B_LED, 0);
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(R_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(R_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(G_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
 btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(G_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(B_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
   btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(B_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  }
  if (Serial.available() > 0) {

    // read the incoming byte:
    inByte = Serial.read();

    // If the marker's found, next 6 characters are the colour
    if (inByte == '#') {

      while (pointer < 6) { // accumulate 6 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }

      // now we have the 3 numbers stored as hex numbers
      // we need to decode them into 3 bytes r, g and b
      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
      pointer = 0; // reset the pointer so we can reuse the buffer
     
    }
  }   

  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn; // val is now old, let's store it

  if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
 
    analogWrite(R_LED, r);  // turn the leds on
    analogWrite(G_LED, g);  // at the colour
    analogWrite(B_LED, b);  // sent by the computer
   
    delay(1000);
  }
   else {

    analogWrite(R_LED, 0);  // otherwise turn off
    analogWrite(G_LED, 0);
    analogWrite(B_LED, 0);
   }
 
  delay(100);                // wait 100ms between each send
}

int hex2dec(byte c) { // converts one HEX character into a number
    if (c >= '0' && c <= '9') {
      return c - '0';
    } else if (c >= 'A' && c <= 'F') {
      return c - 'A' + 10;
    }
}
39  Using Arduino / Programming Questions / Re: Just Finished Getting Started w/ Arduino First Program, Question! on: March 30, 2012, 01:10:41 am
Code:
#define SENSOR 0   
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12

int val = 0; // variable to store the value coming from the sensor

int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;

byte r = 0;
byte g = 0;
byte b = 0;

void setup() {
  Serial.begin(9600);  // open the serial port
  pinMode(BUTTON, INPUT);
}

void loop() {
  val = analogRead(SENSOR); // read the value from the sensor
  Serial.println(val);      // print the value to
                            // the serial port

if (Serial.available() > 0) {

    // read the incoming byte:
    inByte = Serial.read();

    // If the marker's found, next 6 characters are the colour
    if (inByte == '#') {

      while (pointer < 6) { // accumulate 6 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }

      // now we have the 3 numbers stored as hex numbers
      // we need to decode them into 3 bytes r, g and b
      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
      pointer = 0; // reset the pointer so we can reuse the buffer
     
    }
  }   

  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn; // val is now old, let's store it

  if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
 
    analogWrite(R_LED, r);  // turn the leds on
    analogWrite(G_LED, g);  // at the colour
    analogWrite(B_LED, b);  // sent by the computer
  }
  while ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(R_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(R_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(G_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
 btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(G_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(B_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
   btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(B_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  }
  if (Serial.available() > 0) {

    // read the incoming byte:
    inByte = Serial.read();

    // If the marker's found, next 6 characters are the colour
    if (inByte == '#') {

      while (pointer < 6) { // accumulate 6 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }

      // now we have the 3 numbers stored as hex numbers
      // we need to decode them into 3 bytes r, g and b
      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
      pointer = 0; // reset the pointer so we can reuse the buffer
     
    }
  }   

  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn; // val is now old, let's store it

  if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
 
    analogWrite(R_LED, r);  // turn the leds on
    analogWrite(G_LED, g);  // at the colour
    analogWrite(B_LED, b);  // sent by the computer
   
    delay(9899);
  }
   else {

    analogWrite(R_LED, 0);  // otherwise turn off
    analogWrite(G_LED, 0);
    analogWrite(B_LED, 0);
   }
 
  delay(100);                // wait 100ms between each send
}

int hex2dec(byte c) { // converts one HEX character into a number
    if (c >= '0' && c <= '9') {
      return c - '0';
    } else if (c >= 'A' && c <= 'F') {
      return c - 'A' + 10;
    }
}

Changed to a while statement, now I just have to work out the delay because after serial data stops it takes around 20 second for the lights to cycle again.
40  Using Arduino / Programming Questions / Just Finished Getting Started w/ Arduino First Program, Question! on: March 30, 2012, 12:46:43 am
Okay so this is my first attempt at coding something for Arduino myself and it is an extension of the last example of getting started with Arduino.

Code:
#define SENSOR 0   
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12

int val = 0; // variable to store the value coming from the sensor

int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;
int i = 0;

byte r = 0;
byte g = 0;
byte b = 0;

void setup() {
  Serial.begin(9600);  // open the serial port
  pinMode(BUTTON, INPUT);
}

void loop() {
  val = analogRead(SENSOR); // read the value from the sensor
  Serial.println(val);      // print the value to
                            // the serial port

if (Serial.available() > 0) {

    // read the incoming byte:
    inByte = Serial.read();

    // If the marker's found, next 6 characters are the colour
    if (inByte == '#') {

      while (pointer < 6) { // accumulate 6 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }

      // now we have the 3 numbers stored as hex numbers
      // we need to decode them into 3 bytes r, g and b
      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
     
      pointer = 0; // reset the pointer so we can reuse the buffer
     
    }
  }   

  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn; // val is now old, let's store it

  if ((state == 1) && (Serial.available() > 0)) { // if the lamp is on
 
    analogWrite(R_LED, r);  // turn the leds on
    analogWrite(G_LED, g);  // at the colour
    analogWrite(B_LED, b);  // sent by the computer
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
  delay (1000);
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(R_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
  btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(R_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(G_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
 btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(G_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  if ((state == 1) && (Serial.available() == 0)) { // if the lamp is on
 
  for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
    analogWrite(B_LED, i);      // set the LED brightness
    delay(10); // Wait 10ms because analogWrite
               // is instantaneous and we would
               // not see any change
   btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
  }
  for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)

    analogWrite(B_LED, i); // set the LED brightness
    delay(10);           // Wait 10ms
     btn = digitalRead(BUTTON); // read input value and store it

  // Check if there was a transition
  if ((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }
   old_btn = btn; // val is now old, let's store it
   
  }
  }
  }
   else {

    analogWrite(R_LED, 0);  // otherwise turn off
    analogWrite(G_LED, 0);
    analogWrite(B_LED, 0);
   }
 
  delay(100);                // wait 100ms between each send
}

int hex2dec(byte c) { // converts one HEX character into a number
    if (c >= '0' && c <= '9') {
      return c - '0';
    } else if (c >= 'A' && c <= 'F') {
      return c - 'A' + 10;
    }
}

Now the idea is that the three lights will fade in and fade out if there is no serial data, however if there is serial data, then it assigns the brightness from that data. The problem being:

When run without serial everything works as expected. However when run with serial data one light flashes every millisecond for 500 or so milliseconds and then moves onto the next. I believe I have some problem with the if statement that is causing it to run through the fades even when there is serial data.

Any help would be greatly appreciated, thanks in advance.
Pages: 1 2 [3]