Help with programming an Arduino Uno to make an RGB LED blink rapidly, slowly, then off

Thread Starter

-live wire-

Joined Dec 22, 2017
959
I just started programming Arduinos. I am kind of good at programming in general, but mainly do stuff with components like resistors, capacitors, inductors, relays, diodes etc. Micro-controllers are very new to me. I just got an Arduino Uno and have been able to program it to do some stuff.

I have made an RGB LED blink on and off, switch between random colors, and basic things like that (by creating variables, using digital and analog write, delay, and random). I have begun to do stuff with if/then/else statements and analog and digital read. For example, depending on the input from a photoresistor, an RGB LED either turns on and changes between different colors or, if there is already enough light, it turns off.

I want to make it so that you press a push button and the RGB led blinks rapidly, press it again and it blinks slowly, then if pressed one more time it turns off. After turned off it should be able to go though the cycle again. What is the best approach to doing this? Also, some sample code would be helpful.
 

Reloadron

Joined Jan 15, 2015
7,501
You may want to just begin with a Google of "Arduino Button State". As to using a button to, for example, change a LED behavior the following code should give you some ideas: RGB Button Select

C:
const int BlueLed=9;
const int GreenLed=10;
const int RedLed=11;
const int button=8;

boolean beforeCase=LOW;
boolean nowCase=LOW;

int ledMode=0;

  void setup()
{
pinMode(BlueLed,OUTPUT);
pinMode(GreenLed,OUTPUT);
pinMode(RedLed,OUTPUT);
pinMode(button,INPUT);
}

   boolean buttonControl(boolean now)
  {
   boolean presentBtn = digitalRead(button);
   if(now!=presentBtn)
  {
   delay(50);
   presentBtn=digitalRead(button);
  }
   return presentBtn;
  }

    void rgbSet(int colour)
   {
       if(colour==1)
   {
     digitalWrite(RedLed,LOW);
     digitalWrite(GreenLed,HIGH);
     digitalWrite(BlueLed,HIGH);
}
       else if(colour==2)
{
     digitalWrite(RedLed,HIGH);
     digitalWrite(GreenLed,LOW);
     digitalWrite(BlueLed,HIGH);
}
       else if(colour==3)
{
     digitalWrite(RedLed,HIGH);
     digitalWrite(GreenLed,HIGH);
     digitalWrite(BlueLed,LOW);
}
       else if(colour==4)
{
     analogWrite(RedLed,150);
     analogWrite(GreenLed,0);
     analogWrite(BlueLed,150);
}
       else if(colour==5)
{
     analogWrite(RedLed,0);
     analogWrite(GreenLed,50);
     analogWrite(BlueLed,50);
}
}
         void loop()
      {
         nowCase=buttonControl(beforeCase);
         if(beforeCase==LOW && nowCase==HIGH)
      {
         ledMode++;
      }
         beforeCase=nowCase;
         if(ledMode==6)
         ledMode=0;

         rgbSet(ledMode);

}
The code is pretty self explanatory and can easily be modified to do whatever you want to try.

Ron
 
Last edited by a moderator:

nsaspook

Joined Aug 27, 2009
13,080
That was helpful, but I still only kind of understand how I would do it.
Take your time and study it until you understand. I can guarantee that understanding Finite State Machines principles is important to writing better code from simple projects to large complex ones.
 

Thread Starter

-live wire-

Joined Dec 22, 2017
959
So I copied and pasted it and only made a few minor changes. However, I am getting this error message:

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

c:/program files/windowsapps/arduinollc.arduinoide_1.8.10.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/lib/avr5/crtatmega328p.o:(.init9+0x0): undefined reference to `main'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 

Thread Starter

-live wire-

Joined Dec 22, 2017
959
Anyways, I am having trouble with using this ultra-sonic sensor. Here is my code:

C:
const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance= duration*.017 ;

Serial.print("Distance: ");
Serial.println(distance);
}
I am getting this error message:
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

c:/program files/windowsapps/arduinollc.arduinoide_1.8.10.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/lib/avr5/crtatmega328p.o:(.init9+0x0): undefined reference to `main'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I do not see what the problem is. Is it my code, or something else?

Moderators note : used code tags for C
 
Last edited by a moderator:

OBW0549

Joined Mar 2, 2015
3,566
I'm not sure you are aware of this, but Arduino maintain their own very active and extensive discussion forums at http://forum.arduino.cc/ where you could probably get answers to all of your questions very quickly. Although there are some people here on AAC who use Arduinos, using them tends not to be our main suit.
 

Reloadron

Joined Jan 15, 2015
7,501
Using a Windows 10 computer I just uploaded the code I posted and it runs fine using an Arduino Uno. My guess is Windows is having an issue. Also make sure you have the correct board selected under Tools and also under tools make sure you have the correct board selected. The latter is important, making sure the correct serial port is selected.

Lastly I agree with OBW0549 as while many here use the Arduino boards you would get much better targeted help in the actual Arduino Forums for a problem like this.
I'm not sure you are aware of this, but Arduino maintain their own very active and extensive discussion forums at http://forum.arduino.cc/ where you could probably get answers to all of your questions very quickly. Although there are some people here on AAC who use Arduinos, using them tends not to be our main suit.
Let us know what you do find out. I will look at your code.

Ron
 

Reloadron

Joined Jan 15, 2015
7,501
This link is pretty much the code you are using for Ultrasonic Distance, the code is well explained in this article and additionally the code is better understood when the comments and remarks are left in the code:


C:
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
While I do not have the sensor this code loads just fine for me. My guess is you have a Windows 10 issue or something is wrong with your setup.

Nevermind, I see you got things working. :)

Ron
 
Last edited by a moderator:
Top