1

I have been writing a program which requires me to take input from the serial monitor in Arduino. However I'm having some problems. Here's my code. UPDATE: This is the new code I have created after a couple revisions.

#include <SoftwareSerial.h>

int R1 = 20;
int R2_2 = 20;
int R4_7 = 20;
int R5_6 = 20;
int R7_5 = 20;
int R8_2 = 20;
int R10 = 22;
int R15 = 20;
int R22 = 20;
int R27 = 20;
int R33 = 22;
int R39 = 20;
int R47 = 20;
int R56 = 20;
int R68 = 20;
int R75 = 20;
int R82 = 20;
int R100 = 20;
int R120 = 20;
int R150 = 20;
int R180 = 20;
int R220 = 20;
int R270 = 20;
int R330 = 20;
int R390 = 20;
int R470 = 20;
int R510 = 20;
int R680 = 20;
int R820 = 20;
int R1K = 20;
int R1K5 = 20;
int R2K2 = 20;
int R3K3 = 20;
int R3K9 = 20;
int R4K7 = 20;
int R5K6 = 20;
int R6K8 = 20;
int R7K5 = 20;
int R8K2 = 20;
int R10K = 20;
int R15K = 20;
int R22K = 20;
int R33K = 20;
int R39K = 20;
int R47K = 20;
int R56K = 20;
int R68K = 20;
int R75K = 20;
int R82K = 20;
int R100K = 20;
int R150K = 20;
int R180K = 20;
int R220K = 20;
int R330K = 20;
int R470K = 20;
int R560K = 20;
int R680K = 20;
int R1M = 20;
int R1M5 = 20;
int R2M = 20;
int R3M3 = 20;
int R4M7 = 20;
int R5M6 = 20;
int R10M = 20;
int Resistors = R1 + R2_2 + R4_7 + R5_6 + R7_5 + R8_2 + R10 + R15 + R22     + R27 + R33 + R39 + R47 + R56 + R68 + R75 + R82 + R100 + R120 + R150 + R180 + R220 + R270 + R330 + R390 + R470 + R510 + R680 + R820 + R1K + R1K5 + R2K2 + R3K3 + R3K9 + R4K7 + R5K6 + R6K8 + R7K5 + R8K2 + R10K + R15K + R22K + R33K + R39K + R47K + R56K + R68K + R75K + R82K + R100K + R150K + R180K + R220K + R330K + R470K + R560K + R680K + R1M + R1M5 + R2M + R3M3 + R4M7 + R5M6 + R10M;
int AClips = 11;
int Blue_LED = 50;
int RGB_LED = 1;
int Yellow_LED = 10;
int Red_LED = 16;
int Button = 102;
int Carbon_Film = 2;
int Ambient_Light_Sensor = 10;
int Laser_Diode = 9;
int photocell = 1;
int Piezo_Buzzer = 1;
int Relay = 1;
int Transistor_2N2222A = 1;
int Transistors = 0 + Transistor_2N2222A;
int Servo = 1;
int Diode_1N4148 = 2;
int Diodes = Diode_1N4148;

//This next line is the line that was highlighted
read.Serial(searchedItem);

void setup() {
//setup code here, to run once:
Serial.begin(9600);
}

void loop() {
//loop code here, to run forever:
if (Serial.available() > 0) {
  // read the incoming byte:
  searchedItem = Serial.read();
  //Code here
  if (searchedItem == "Red LED", "red led", "Red led") {
    searchedItem = Red_LED;
    if (searchedItem => 1) {
     Serial.println("\nFound!\nYou have ", Red_LED, "Red LEDs");
      }
    }
  }
}

2 Answers2

0

You have code outside of functions. You can't do that, that's why the line was "highlighted."

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • So I moved those 2 lines of code to the loop area and now instead it's displaying "Read was not declared in this scope" – tobasco7454 Jul 02 '15 at 06:24
0

This code isn't inside any function:

read.Serial(searchedItem);

Moreover, the function is Serial.read. And you need to ensure that there are available data in the buffer with Serial.available() Try something like that inside loop():

if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    //Your code here
}
jabujavi
  • 477
  • 5
  • 15
  • Quick Question, by "This code isn't inside any function" do you mean the code should be in a function or not? Also if it should be in a function, loop or setup? – tobasco7454 Jul 02 '15 at 08:27
  • Arduino works executing first "setup()" and then loops over "loop()" so any code out of this functions only could be a declaration of a function or variable. It's like you have something like: main(){setup(); while(true){loop();}} in a tipycal C/C++ program. – jabujavi Jul 02 '15 at 09:55
  • So to sum it up, should I put the Serial.read(searchedItem); in the setup? @jabujavi – tobasco7454 Jul 03 '15 at 03:28
  • NOPE... You must put searchedItem = Serial.read() in loop. Arduino will run it infinitly. – jabujavi Jul 03 '15 at 06:07
  • I already have that. This is starting to get confusing so I will edit the original question's code to be what I currently have so from there it will be easier to figure out the correct changes. @jabujavi – tobasco7454 Jul 03 '15 at 08:39
  • ok. With your new code your are reading just one byte/char. You need to read until you get '\n' and store each caracter in a string for compare it. – jabujavi Jul 03 '15 at 09:38
  • I'm still used to this language and really any language and I'm not sure how to do that. Could take the time to edit the code in the question to what your explaining? Sorry for the inconvenience. – tobasco7454 Jul 05 '15 at 20:36
  • Sorry, but I haven't got this time. If you don't have experience programming try something easier for start... – jabujavi Jul 06 '15 at 09:43