Control LED

I have code like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #pragma endregion
	int counter = 0;
	int dec_value = 0;

	private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) 
	{
		int a;
		for (a = 0; a < 2; a++)
		{
			button2->PerformClick();
		}

		String^ num = textBox2->Text;
		String^ rep = num->Replace("\r", "");
		int base = 1;
		int len = rep->Length;
		for (int i = len - 1; i >= 0; i--)
		{
			if (rep[i] == '1')
				dec_value += base;
			base = base * 2;
		}
		textBox3->AppendText("Decimal: " + dec_value);
		return;
	}

	private: System::Void Button2_Click(System::Object^ sender, System::EventArgs^ e) 
	{
		SerialPort port("COM9", 9600);
		port.Open();
		while (true)
		{
			array<Int32>^ numbers = gcnew array<Int32>{61,62,64};
			for (int i = 0; i < 3; i++)
			{
				textBox1->Text += numbers[i].ToString() + " ";
				port.Write(textBox1->Text);
				mStr = port.ReadLine();
				if (counter != 0)
					textBox2->Text += mStr;
			}
			if (counter == 0)
				counter = 1;
			else
				counter = 0;
			
			if (dec_value >= 100000)
				port.Write("A");
			else if (dec_value < 100000)
				port.Write("B");
			else
				port.Write("C");
				
			return;
		}
	}


The purpose is I want to control LED based the value of "dec_value" but it doesn't work

And the arduino program is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <Eeprom24Cxx.h>
#include <Wire.h>

#define ledRed A0
#define ledGreen A1
#define ledBlue A3

static Eeprom24C eeprom(4, 0x50);
int i =0;
long valueCounterAll= 0;

void setup() 
{ 
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);

  digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledBlue, LOW);
  
  Serial.begin(9600);
}

void loop() 
{
  if (Serial.available())
  {
    i = Serial.parseInt();
    byte data = eeprom.read_1_byte(i);
    for (int i = 0; i < 8; i++)
    {
      Serial.print(bitRead(data, 7 - i));
    }
    Serial.println("");
  }

  if(valueCounterAll>=10000000)
    {
      digitalWrite(ledRed, HIGH);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledBlue, HIGH);
    }
    else if (valueCounterAll<10000000)
    {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledBlue, HIGH);
    }
    else
    {
      digitalWrite(ledRed, HIGH);
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledBlue, LOW);
    }
}

Is there help, anyone ?
Last edited on
On line 29 of the arduino program you read an int using parseInt(). While in you pc program you send the letters "A", "B" and "C". What does i contain after Serial.parseInt()?

Also you never change valueCounterAll at all. And how will the thiird else on line 50 ever be processed?
Topic archived. No new replies allowed.