my project consists out of a visual studio application and an Arduino. The program has multiple options for a light color and an additional disco mode ( automatic switching between colors). The normal options work fine when you press them before the disco option, but whenever the disco button is clicked, it won't go out of the loop anymore when another button is pressed.
Arduino Code
int RedLight = 13;
int GreenLight = 12;
int BlueLight = 11;
unsigned long lasttime = 0;
unsigned long interval = 2000;
unsigned long currenttime = millis();
int colorMode = 0;
int message = 0;
void setup() {
Serial.begin(9600) ;
pinMode(RedLight, OUTPUT) ;
pinMode(GreenLight, OUTPUT) ;
pinMode(BlueLight, OUTPUT) ;
colorSetting(LOW, LOW, LOW);
}
void loop() {
if (Serial.available() > 0) { // Check to see if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == 'R') { // RED
colorSetting(255, 0, 0);
Serial.println("Red Light");
} // Send back LED on
if (message == 'Y') { // YELLOW
colorSetting(255, 0, 255);
Serial.println("Yellow Light");
} // Send back LED on
if (message == 'G') { //GREEN
colorSetting(0, 0, 255 );
Serial.println("Green Light"); // Send back LED on
}
if (message == 'C') { //CYAN
colorSetting(0, 255, 255);
Serial.println("Cyan Light"); // Send back LED on
}
if (message == 'B') { //BLUE
colorSetting(0, 255, 0);
Serial.println("Blue Light"); // Send back LED on
}
if (message == 'P') { //PINK
colorSetting(255, 255, 0);
Serial.println("Pink Light"); // Send back LED on
}
if (message == 'W') { //WHITE
colorSetting(255, 255, 255);
Serial.println("White Light"); // Send back LED on
}
do {
discoMode();
Serial.println("Disco Light"); // Send back LED on
} while (message == 'D');
}
}
void discoMode() {
unsigned long currenttime = millis();
if (currenttime - lasttime >= interval) {
colorMode++;
if (colorMode > 7) {
colorMode = 1;
}
lasttime = currenttime;
}
if (colorMode == 1) {
colorSetting(255, 0, 0);
}
if (colorMode == 2) {
colorSetting(255, 0, 255);
}
if (colorMode == 3) {
colorSetting(0, 0, 255 ) ;
}
if (colorMode == 4) {
colorSetting(0, 255, 255);
}
if (colorMode == 5) {
colorSetting(0, 255, 0);
}
if (colorMode == 6) {
colorSetting(255, 255, 0);
}
if (colorMode == 7) {
colorSetting(255, 255, 255);
}
}
void colorSetting(int red, int green, int blue) //writes an analog value to a pin
{ // making use of common anode, so have to substract the color value from 255 in order to set the rgb color
analogWrite( RedLight, 255 - red ) ;
analogWrite( GreenLight, 255 - green ) ;
analogWrite( BlueLight, 255 - blue ) ;
}
Visual studio code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.Open();
}
private void BtRed_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("R");
LbColor.Text = "The current color is Red";
}
private void BtBlue_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("B");
LbColor.Text = "The current color is Blue";
}
private void BtGreen_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("G");
LbColor.Text = "The current color is Green";
}
private void BtYellow_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("Y");
LbColor.Text = "The current color is Yellow";
}
private void BtCyan_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("C");
LbColor.Text = "The current color is Cyan";
}
private void BtPink_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("P");
LbColor.Text = "The current color is Pink";
}
private void BtWhite_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("W");
LbColor.Text = "The current color is White";
}
private void BtDisco_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("D");
LbColor.Text = "The current color is disco";
}
}
}