Jump to content

please help me with my Arduino code

Guest

Can anyone help me with some Arduino code? i'm completely stuck.

The basic idea is that I will be able to control the RGB led strip under my desk and in my pc with my pc. i made a quick image of what i had in mind for the visual studio program (which my friend could do hopefully :D) here it is:

lighting.png

 

This is the Arduino code, and the basic idea is that through serial communication the visual program will send a value of:

- 1 for off

- 2 for on

- 3 for color in the format of: 3,255,255,255, 3/255/255/255 or some other divider.

the original Instructable code didn't work either.

 

I have a green led hooked up to pwm pin 5,

a red led hooked up to pwm pin 3

and finally a yellow (dammit where's my blue led? :D) led hooked up to pwm pin 6.

 

I tried to bodge together the readASCIIString built in code, but that didn't work either.

here are both:

int redpin = 3; //intialize all the pins
int greenpin = 5;
int bluepin = 6;
int r = 255; //initialize the rgb values
int g = 255;
int b = 255;
int serial; //initialize the variable to store the serial input 

void setup() {
  pinMode(redpin, OUTPUT); //set the pins as output
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  analogWrite(redpin, 0); //turn the pins off
  analogWrite(greenpin, 0);
  analogWrite(bluepin, 0);

  Serial.begin(9600); //begin the serial monitor at 9600 baud
  Serial.print("Hello"); //say Hello, be polite!
}

void loop() {
    int serial = Serial.read();
    if (Serial.available() > 0)
    if (serial == 0) //if it's 0 the pins turn off
    {
      analogWrite(redpin, 0); //turn the pins off
      analogWrite(greenpin, 0);
      analogWrite(bluepin, 0);
      }
    else if (serial == 1)//if it's 1 the pins turn on
    {
      analogWrite(redpin, r); //turn the pins on
      analogWrite(greenpin, g);
      analogWrite(bluepin, b);
      }
    else if (serial == 3)//if it's 3 change the colours
    {
      int r = Serial.parseInt();//look for the next valid integer in the incoming serial stream
      int g = Serial.parseInt();//do it again
      int b = Serial.parseInt();//and again
      }
    }

 

#define RedLED 3
#define GreenLED 6
#define BlueLED 5

boolean powerOn = false;

int r = 0;
int g = 0;
int b = 0;

boolean blinking = false; //Whether or not we should be blinking the light
boolean blinkState = false; //When we are blinking, is the light supposed to be on (true) or off (false)
unsigned long blinkInterval = 1000; //How long we want between blinks.
long lastBlink = 0;

char inData;

void setup()
{
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  analogWrite(RedLED, 255); //Start out with the light off
  analogWrite(GreenLED, 255);
  analogWrite(BlueLED, 255);
  
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) //If there is data coming from the pc
  {
     inData = Serial.read() - '10';
     
     if (inData == 0) // 0 means turn light off
     {
         togglePower(false); 
     }
     else if (inData == 1) // 1 means turn light on
     {
        togglePower(true); 
     }
     else if (inData == 3) // 3 means change colors
     {
       r = 255 - getIncomingData();
       g = 255 - getIncomingData();
       b = 255 - getIncomingData();
       
       //Serial.println(r); //For testing purposes
       //Serial.println(g);
       //Serial.println(b); 
       
       if (powerOn)
       {
         updateColors();
       }
     }
     else if (inData == 4) // 4 means turn off blink mode
     {
       blinking = false;
       updateColors();
       //Serial.println("Blink mode off"); //For testing purposes
     }
     else if (inData == 5) // 5 means turn on blink mode
     {
       blinkInterval = getIncomingData();
       blinking = true;
       //Serial.println("Blink mode on "); //For testing purposes
       //Serial.println(blinkInterval); //For testing purposes
     }

  }
  
  //This section controls the blinking
  if (blinking && powerOn)
  {
    if (millis() - lastBlink >= blinkInterval)
    {
      lastBlink = millis();
      if (blinkState)
      {
        updateColors();
      }
      else if (!blinkState)
      {
        turnLEDoff();
      }
      
      blinkState = !blinkState;
    }
  }
}

void togglePower(boolean on)
{
  if (!on)
  {
    powerOn = false;
    
    turnLEDoff();
    
    //Serial.println("Power is off"); //For testing purposes
  }
  else if (on)
  {
    powerOn = true;
    updateColors(); //This turns on the rgb LED
    //Serial.println("Power is on"); //For testing purposes
  }
}

void turnLEDoff()
{
    //For thie RGB led, 255 is what we think of as 0.
    digitalWrite(RedLED, 255);
    digitalWrite(GreenLED, 255);
    digitalWrite(BlueLED, 255);
}

// Credit for this section goes to ROBOTMAN at Trossen Robotics Community forums.
int getIncomingData()
{
  inData = 0;
  int newNumber = 0;
  
  while (inData != '/')
  {
    inData = Serial.read();
    if (inData > 0 && inData != '/')
    {
       newNumber = newNumber * 10 + inData - '0';
    }
  }
  
  return newNumber;
}

void updateColors()
{
 analogWrite(RedLED, r);
 analogWrite(GreenLED, g);
 analogWrite(BlueLED, b);
}

Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

Do you have made some simple communication tries? Have you tried to send and receiver bytes so you get used to it?

 

It may be many things why it is not working, I don't know do you handle serial communication on C# side. It can be that you send 2 as character which is byte 0x32 and not 0x02 byte.

Link to comment
Share on other sites

Link to post
Share on other sites

Quote

inData = Serial.read() - '10';

'10' as a character literal causes it to overflow and probably isn't doing what you expect it to. Other than that, it's hard to help if you don't say what the problem you're having is.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

On 19/02/2017 at 10:23 PM, fizzlesticks said:

'10' as a character literal causes it to overflow and probably isn't doing what you expect it to. Other than that, it's hard to help if you don't say what the problem you're having is.

No, thanks for the reply, but I went over to eevblog and they helped me out already. Topic closed but thanks anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×