Go Down

Topic: Combining codes (Read 1 time) previous topic - next topic

Gerdelamora

I just don't understand, I need to combine these codes in one, I have tried since the morning and I cant get what I want. I need help please. Thanks.

Code: [Select]
#include <Servo.h>
#include <ADJDS311.h>
#include <Wire.h>

int sensorLed_pin = 2; //LED on the ADJDS-311
ADJDS311 colorSensor(sensorLed_pin);

//if using an RGB LED (Needs PWM Pins)

Servo myservo;
Servo myservo2;
Servo myservo3;

void setup(){
  Serial.begin(9600);
 
  colorSensor.init();
  colorSensor.ledOn(); //turn LED on
  myservo.attach(10);
  myservo2.attach(8);
  myservo3.attach(9);

  //Calibrate white
  //Need to hold white card in front (1-3mm) of it to calibrate from
  colorSensor.calibrate();
}

void loop(){
  RGBC color = colorSensor.read(); //read the color
 
  Serial.print(color.red);
  Serial.print(" | ");
  Serial.print(color.green);
  Serial.print(" | ");
  Serial.print(color.blue);
  Serial.print(" | ");
  Serial.println(color.clear);
  delay(1100); //just here to slow down the serial output
 
 
   if(color.red > 980 && color.green < 1000) {  //----------------rojo-----//
    myservo.write(180);
  }
  else{
    myservo.write(0);
}
if ( color.red < 500 && color.blue < 380 && color.clear < 330 && color.green > 350 && color.green <450){    //---------------verde----------//
  myservo2.write(90);
}
else{
  myservo2.write(0);
}
if (color.red > 400 && color.red < 600 && color.blue > 260 && color.green < 1000){//----------azul------//
  myservo3.write(90);
}
else{
  myservo3.write(0);
}


Code: [Select]
#include <Servo.h>

int fsrPin = 0;
int fsrReading;

Servo myservo4;
Servo myservo5;
Servo myservo6;

void setup(){
   Serial.begin(9600);
   
  myservo4.attach(7);
  myservo5.attach(5);
  myservo6.attach(6);
  myservo4.write(90);
  myservo5.write(180);
  myservo6.write(180);
 
}
 
  void loop(){
   
  fsrReading = analogRead(fsrPin);
  Serial.print ("FSR Reading = ");
  Serial.print(fsrReading);
  delay(1100);
 
    if (fsrReading < 5){
    myservo4.write(90);
    myservo5.write(180);
    myservo6.write(180);
    }
  if (fsrReading > 440 && fsrReading < 470) {
    myservo4.write(120);
    delay(2000);
    myservo6.write(0);
    delay(500);
    myservo6.write(180);
    delay(500);
    myservo6.write(0);
  }
   
   if (fsrReading > 455 && fsrReading < 576){
     myservo5.write(20);
  myservo4.write(0);
  delay(1000);
  myservo6.write(0);
  delay(500);
  myservo6.write(180);
    delay(500);
    myservo6.write(0);
   
}

if (fsrReading > 50 && fsrReading < 340) {
  myservo4.write(65);
  delay(1000);
  myservo5.write(10);
  delay(1000);
  myservo6.write(0);
  delay(1000);
   myservo6.write(180);
    delay(500);
    myservo6.write(0);
}

if (fsrReading > 576) {
  myservo4.write(175);
  delay(1000);
  myservo6.write(0);
  delay(1000);
   myservo6.write(180);
    delay(500);
    myservo6.write(0);
}
  }


Code: [Select]
int sensorPin = 2; //analog pin 0
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13


void setup(){
  Serial.begin(9600);
   pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  analogWrite(pwm_a, 60);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, 60);
}

void loop(){
  int val = analogRead(sensorPin);
  Serial.println(val);

  //just to slow down the output - remove if trying to catch an object passing by
  delay(100);

if (val > 300);
{
     analogWrite(pwm_a, 60);// movimients ardumoto.//
  analogWrite(pwm_b, 60);// movimients ardumoto.//                                                           
  digitalWrite(dir_a, HIGH);// movimients ardumoto.//
  digitalWrite(dir_b, HIGH);// movimients ardumoto.//
}
else{
  analogWrite(pwm_a, 60);// movimients ardumoto.//
  analogWrite(pwm_b, 60);// movimients ardumoto.//
 
}



tgm1175

This is some really messy code, and there is definitely a lot of room for some optimizing.  But if the only goal was to combine all 3 pieces of code into one, here you go:

Keep in mind, you're using pin 2 twice - once as sensorLed_pin, and once as sensorPin.  Be careful there.


Code: [Select]


#include <Servo.h>
#include <ADJDS311.h>
#include <Wire.h>

int sensorLed_pin = 2; //LED on the ADJDS-311
ADJDS311 colorSensor(sensorLed_pin);

//if using an RGB LED (Needs PWM Pins)
int fsrPin = 0;
int fsrReading;
Servo myservo;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
int sensorPin = 2; //analog pin 0
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13



void setup(){
  Serial.begin(9600);

  colorSensor.init();
  colorSensor.ledOn(); //turn LED on
  myservo.attach(10);
  myservo2.attach(8);
  myservo3.attach(9);
  myservo4.attach(7);
  myservo5.attach(5);
  myservo6.attach(6);
  myservo4.write(90);
  myservo5.write(180);
  myservo6.write(180);
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  analogWrite(pwm_a, 60);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, 60);

  //Calibrate white
  //Need to hold white card in front (1-3mm) of it to calibrate from
  colorSensor.calibrate();


}

void loop(){
  loop_code1();
  loop_code2();
  loop_code3();
}

void loop_code1()
{
  RGBC color = colorSensor.read(); //read the color

  Serial.print(color.red);
  Serial.print(" | ");
  Serial.print(color.green);
  Serial.print(" | ");
  Serial.print(color.blue);
  Serial.print(" | ");
  Serial.println(color.clear);
  delay(1100); //just here to slow down the serial output


  if(color.red > 980 && color.green < 1000) {  //----------------rojo-----//
    myservo.write(180);
  }
  else{
    myservo.write(0);
  }
  if ( color.red < 500 && color.blue < 380 && color.clear < 330 && color.green > 350 && color.green <450){    //---------------verde----------//
    myservo2.write(90);
  }
  else{
    myservo2.write(0);
  }
  if (color.red > 400 && color.red < 600 && color.blue > 260 && color.green < 1000){//----------azul------//
    myservo3.write(90);
  }
  else{
    myservo3.write(0);
  }
}

void loop_code2()
{

  fsrReading = analogRead(fsrPin);
  Serial.print ("FSR Reading = ");
  Serial.print(fsrReading);
  delay(1100);

  if (fsrReading < 5){
    myservo4.write(90);
    myservo5.write(180);
    myservo6.write(180);
  }
  if (fsrReading > 440 && fsrReading < 470) {
    myservo4.write(120);
    delay(2000);
    myservo6.write(0);
    delay(500);
    myservo6.write(180);
    delay(500);
    myservo6.write(0);
  }

  if (fsrReading > 455 && fsrReading < 576){
    myservo5.write(20);
    myservo4.write(0);
    delay(1000);
    myservo6.write(0);
    delay(500);
    myservo6.write(180);
    delay(500);
    myservo6.write(0);

  }

  if (fsrReading > 50 && fsrReading < 340) {
    myservo4.write(65);
    delay(1000);
    myservo5.write(10);
    delay(1000);
    myservo6.write(0);
    delay(1000);
    myservo6.write(180);
    delay(500);
    myservo6.write(0);
  }

  if (fsrReading > 576) {
    myservo4.write(175);
    delay(1000);
    myservo6.write(0);
    delay(1000);
    myservo6.write(180);
    delay(500);
    myservo6.write(0);
  }
}

void loop_code3()
{
  int val = analogRead(sensorPin);
  Serial.println(val);

  //just to slow down the output - remove if trying to catch an object passing by
  delay(100);
  analogWrite(pwm_a, 60);// movimients ardumoto.//
  analogWrite(pwm_b, 60);// movimients ardumoto./

  if (val > 300)
  {                                                           
    digitalWrite(dir_a, HIGH);// movimients ardumoto.//
    digitalWrite(dir_b, HIGH);// movimients ardumoto.//
  }

}

lloyddean

Your third code section is missing a closing curly brace at the very least and perhaps more as well.

tgm1175


Your third code section is missing a closing curly brace at the very least and perhaps more as well.


There was also a random ';' tagged onto the last if statement.  Fixed both for him when I re-posted.

Was REALLY tempted to re-write that terrifying if-block in the 2nd code... It still bothers me.

Gerdelamora

So easy and I didn't realize it. thanks.

Go Up