Go Down

Topic: How do i combine two Arduino sketches? (Read 634 times) previous topic - next topic

Burningred

First Arduino sketch:

void setup(){
  pinMode(0,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}
void loop(){
  int i;
  for( i = 0; i < 10; i++)
  {
    rakamgoster(i);
    delay(500);
  }
}
void rakamgoster(int rakam)
{
  switch(rakam)
  {
    case 0:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    break;
    case 1:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
    case 2:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
    case 3:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
    case 4:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 5:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 6:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 7:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
    case 8:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 9:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  }
}


Second Arduino sketch:

int butonpin = 8;

int gelen = HIGH;
int okunan;
int onceki = LOW;

long zaman = 0;
long ziplama = 200;

void setup()
{
  pinMode(butonpin, INPUT);
  pinMode(0, OUTPUT);
}

void loop()
{
  okunan = digitalRead(butonpin);
  if(okunan == HIGH && onceki == LOW && millis() - zaman > ziplama)
  {
    if (gelen == HIGH)
    gelen = LOW;
    else
    gelen = HIGH;
   
    zaman = millis();
  }
 
  digitalWrite(0, gelen);
  onceki = okunan;
}

Can someone please help?
Thank you!! Appreciate much!!

codlink

Sweet tutorial: http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

When posting code on the forums, please use code tags around the code. (The # button in your editor)
Makes the code look nice and pretty.
//LiNK

CrossRoads

#2
Dec 15, 2012, 05:00 am Last Edit: Dec 15, 2012, 05:02 am by CrossRoads Reason: 1
Sure.
Combine the pre-setup stuff,
Combine the void setup stuff,
Combine the void loop stuff.
Resolve any duplication.

Code: [Select]

int butonpin = 8;

int gelen = HIGH;
int okunan;
int onceki = LOW;

long zaman = 0;
long ziplama = 200;

void setup(){

  pinMode(butonpin, INPUT);
  pinMode(0,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}
void loop(){
  int i;
  for( i = 0; i < 10; i++)
  {
    rakamgoster(i);
    delay(500);
  }
  okunan = digitalRead(butonpin);
  if(okunan == HIGH && onceki == LOW && millis() - zaman > ziplama)
  {
    if (gelen == HIGH)
      gelen = LOW;
    else
      gelen = HIGH;

    zaman = millis();
  }

  digitalWrite(0, gelen);
  onceki = okunan;
}
void rakamgoster(int rakam)
{
  switch(rakam)
  {
  case 0:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    break;
  case 1:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
  case 2:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
  case 3:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
  case 4:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  case 5:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  case 6:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  case 7:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
  case 8:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  case 9:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  }

}

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Burningred

i am totally newbie in programing thank you so much for ur help

Burningred

#4
Dec 15, 2012, 05:57 am Last Edit: Dec 15, 2012, 06:04 am by Burningred Reason: 1
this code didnt fix my problem  =(
code 1 is a led connection by using pushbutton as a switch
Code: [Select]
int butonpin = 8;

int gelen = HIGH;
int okunan;
int onceki = LOW;

long zaman = 0;
long ziplama = 200;

void setup()
{
 pinMode(butonpin, INPUT);
 pinMode(0, OUTPUT);
}

void loop()
{
 okunan = digitalRead(butonpin);
 if(okunan == HIGH && onceki == LOW && millis() - zaman > ziplama)
 {
   if (gelen == HIGH)
   gelen = LOW;
   else
   gelen = HIGH;
   
   zaman = millis();
 }
 
 digitalWrite(0, gelen);
 onceki = okunan;
}


code 2 is a (1-9) chronometer on seven segment and i want to add this a on/off button by using code 1

Code: [Select]
void setup(){
  pinMode(0,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}
void loop(){
  int i;
  for( i = 0; i < 10; i++)
  {
    rakamgoster(i);
    delay(500);
  }
}
void rakamgoster(int rakam)
{
  switch(rakam)
  {
    case 0:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    break;
    case 1:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
    case 2:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
    case 3:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    break;
    case 4:
    digitalWrite(0,HIGH);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 5:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 6:
    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 7:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    break;
    case 8:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
    case 9:
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    break;
  }
}


PaulS

You have two sketches that you say work correctly.

You haven't clearly identified which code is the combined code, nor have you defined what the combined code does, what you want it to do, or how those two differ.

So, no, no help.

Burningred

i want to make a button for chronometer
using code 1
to make it stop/start

isn't it clear yet?  :smiley-red:

CrossRoads

Need to define better what you want to happen.
What is the combined void loop not doing that you want it to do?
Code: [Select]

void loop(){
 int i;
 for( i = 0; i < 10; i++)
 {
   rakamgoster(i);
   delay(500);
 }
 okunan = digitalRead(butonpin);
 if(okunan == HIGH && onceki == LOW && millis() - zaman > ziplama)
 {
   if (gelen == HIGH)
     gelen = LOW;
   else
     gelen = HIGH;

   zaman = millis();
 }

 digitalWrite(0, gelen);
 onceki = okunan;
}

I imagine the delay(500) may be preventing the responsiveness that you wanted.
Probably need to use millis() there like you have done for okunan.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up