Show Posts
Pages: 1 ... 52 53 [54] 55 56 ... 242
796  Using Arduino / Project Guidance / Re: Arduino + Color Sensor + Lock mechanism on: March 25, 2014, 05:43:33 pm
I have this code that works with a color sensor and a servo that you might be able to use. Note: this code uses blocking delays, so you will need to change them to use the millis function. (If need, that is)

Code:
int s0=3,s1=4,s2=5,s3=6;
int flag=0;
int counter=0;
int countR=0,countG=0,countB=0;
void setup()
{
  Serial.begin(115200);
  pinMode(s0,OUTPUT);
  pinMode(s1,OUTPUT);
  pinMode(s2,OUTPUT);
  pinMode(s3,OUTPUT);
}
void TCS()
{
  digitalWrite(s1,HIGH);
  digitalWrite(s0,LOW);
  flag=0;
  attachInterrupt(0, ISR_INTO, CHANGE);
  timer2_init();
}
void ISR_INTO()
{
  counter++;
}
void timer2_init(void)
{
  TCCR2A=0x00;
  TCCR2B=0x07; //the clock frequency source 1024 points
  TCNT2= 100;    //10 ms overflow again
  TIMSK2 = 0x01; //allow interrupt
}
int i=0;
ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function
{
  TCNT2=100;
  flag++;
  if(flag==1)
  {
    counter=0;
  }
  else if(flag==2)
  {
    digitalWrite(s2,LOW);
    digitalWrite(s3,LOW);
    countR=counter/1.051;
    Serial.print("red=");
    Serial.println(countR,DEC);
    digitalWrite(s2,HIGH);
    digitalWrite(s3,HIGH);   
  }
  else if(flag==3)
  {
    countG=counter/1.0157;
    Serial.print("green=");
    Serial.println(countG,DEC);
    digitalWrite(s2,LOW);
    digitalWrite(s3,HIGH);
  }
  else if(flag==4)
  {
    countB=counter/1.114;
    Serial.print("blue=");
    Serial.println(countB,DEC);
    digitalWrite(s2,LOW);
    digitalWrite(s3,LOW);
  }
  else
  {
    flag=0;
    TIMSK2 = 0x00;
  }
  counter=0;
  delay(2);
}
void loop()
{
  delay(10);
  TCS();
  if((countR>10)||(countG>10)||(countB>10))
  {
    if((countR>countG)&&(countR>countB))
    {
      Serial.print("red");
      Serial.print("\n");
      delay(1000); // give time for the servo to move to its correct location.
    }
    else if((countG>=countR)&&(countG>countB))
    {
      Serial.print("green");
      Serial.print("\n");
      delay(1000);
    }
    else if((countB>countG)&&(countB>countR))
    {
      Serial.print("blue");
      Serial.print("\n");
      delay(1000);
    }
  }
  else
  {
    // Do something here otherwise
    // delay(1000);       
  }
}
797  Using Arduino / Project Guidance / Re: Displaying two analog gauges onto an LCD screen. on: March 25, 2014, 05:39:42 pm
A case? not that i'm aware of, you will have to check the site that sells them. If anything you will need to make your own case too.
798  Using Arduino / Project Guidance / Re: Displaying two analog gauges onto an LCD screen. on: March 25, 2014, 05:28:07 pm
Yea its possible but you wont be able to write it in java, it will need to be in C/C++.

Quote
Can data be read from the input at a rate of 500ms, possibly faster?
It can read the data a lot fast then that if you need it to.

Quote
What are the limitations of the video out? I'm thinking of using composite out.
You may want to look into the TVOut library, but it wont be in color. Or you can get a TFT lcd and make your own interface. I have a code that already allows you to make a working gauge, but it will require you to have a TFT that uses the UTFT graphics library.
799  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 10:05:23 pm
This should be better. Instead of "debouncing" the button, I changed it to lockout the button if it remains pressed. This should insure a better toggle on the receiving end.

Code:
#include <VirtualWire.h>

byte ButtonState[4];
byte lastState[4] = {
  LOW};
unsigned long oldTime[4] = {
  0,0,0,0};
byte myButtons[4] = {
  8,9,10,11};

void setup() {
  Serial.begin(115200);      // Debugging
  Serial.println("setup");

  // inisial IO dan ISR
  vw_set_ptt_inverted(true); // dibutuhkan untuk DR3100
  vw_setup(2000);     // Bits per sec
  vw_set_tx_pin(3);

  for(byte i = 0; i < 4; i++)
    pinMode(myButtons[i], INPUT);

  pinMode(13, OUTPUT);
}

void loop()
{
  char *msg = "0"  ;

  if(debounce(0))
  {
    msg = "1";
    Serial.println("1");
    digitalWrite(13, true);
  }
  if(debounce(1)){
    msg = "2";
    Serial.println("2");
    digitalWrite(13, true);
  }
  if(debounce(2))
  {
    msg = "3";
    Serial.println("3");
    digitalWrite(13, true);
  }
  if(debounce(3))
  {
    msg = "4";
    Serial.println("4");
    digitalWrite(13, true);
  }

  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  digitalWrite(13, false);
}

boolean debounce(byte Button)
{
  boolean Output = false;

  ButtonState[Button] = digitalRead(myButtons[Button]);

  if(ButtonState[Button] == HIGH && ButtonState[Button] != lastState[Button])
  {
    Output = true;
  }
  
  lastState[Button] = ButtonState[Button];
  return Output;
}
800  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 09:46:52 pm
The "static" in the debounce function was messing it up for some reason.
Copy and paste the update code.
801  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 09:18:33 pm
Your current issue is the button is bouncing, so if I add in a debounce function, it should fix it.
Code:
#include <VirtualWire.h>

byte ButtonState[4];
byte lastState[4] = {LOW};
unsigned long oldTime[4] = {0,0,0,0};
byte myButtons[4] = {8,9,10,11};

void setup() {
  Serial.begin(9600);      // Debugging
  Serial.println("setup");

  // inisial IO dan ISR
  vw_set_ptt_inverted(true); // dibutuhkan untuk DR3100
  vw_setup(2000);     // Bits per sec
  vw_set_tx_pin(3);
 
  for(byte i = 0; i < 4; i++)
    pinMode(myButtons[i], INPUT);

  pinMode(13, OUTPUT);
}

void loop()
{
  char *msg;

  if(debounce(0))
  {
    msg = "1";
    Serial.println("1");
    digitalWrite(13, true);
  }
  else if(debounce(1)){
    msg = "2";
    Serial.println("2");
    digitalWrite(13, true);
  }
  else if(debounce(2))
  {
    msg = "3";
    Serial.println("3");
    digitalWrite(13, true);
  }
  else if(debounce(3))
  {
    msg = "4";
    Serial.println("4");
    digitalWrite(13, true);
  }
  else
    msg = "0";
   
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  digitalWrite(13, false);
}

boolean debounce(byte Button)
{
  boolean Output = false;
 
 ButtonState[Button] = digitalRead(myButtons[Button]);

  if(ButtonState[Button] != lastState[Button])
  {
    if(ButtonState[Button] == HIGH)
    {
      oldTime[Button] = millis();
    }
    lastState[Button] = ButtonState[Button];
  }

  if((millis() - oldTime[Button] ) > 20UL)
  {
    if(ButtonState[Button] == HIGH)
    {
      Output = true;
    }
  }
 
 return Output;
}
802  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 07:03:42 pm
Post a picture of how your button is wired.
803  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 07:00:24 pm
Try this, it should work now. You should see on onboard led next to pin 13 come on when you press a button.
Code:
#include <VirtualWire.h>

void setup()
{
  Serial.begin(9600);      // Debugging
  Serial.println("setup");

  // inisial IO dan ISR
  vw_set_ptt_inverted(true); // dibutuhkan untuk DR3100
  vw_setup(2000);     // Bits per sec
  vw_set_tx_pin(3);

  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
}

void loop()
{
  char *msg;

  if(digitalRead(8) == HIGH)
  {
    msg = "1";
    digitalWrite(13, true);
  }
  else if(digitalRead(9) == HIGH){
    msg = "2";
    digitalWrite(13, true);
  }
  else if(digitalRead(10) == HIGH)
  {
    msg = "3";
    digitalWrite(13, true);
  }
  else if(digitalRead(11) == HIGH)
  {
    msg = "4";
    digitalWrite(13, true);
  }
  else
    msg = "0";
   
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  digitalWrite(13, false);

}
804  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 06:34:13 pm
If you press the button, does it go HIGH or LOW?

This here in the transmitter code is setting the buttons HIGH and wanting them to go low to send out a message. You might need to comment this part out, but first what do you have?
Quote
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
805  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 05:18:08 pm
Try this.

Tx:
Code:
#include <VirtualWire.h>

void setup()
{
  Serial.begin(9600);      // Debugging
  Serial.println("setup");

  // inisial IO dan ISR
  vw_set_ptt_inverted(true); // dibutuhkan untuk DR3100
  vw_setup(2000);     // Bits per sec
  vw_set_tx_pin(3);

  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);

  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}

void loop()
{
  char *msg;

  if(digitalRead(8) == LOW)
  {
    msg = "1";
    digitalWrite(13, true);
  }
  else if(digitalRead(9) == LOW){
    msg = "2";
    digitalWrite(13, true);
  }
  else if(digitalRead(10) == LOW)
  {
    msg = "3";
    digitalWrite(13, true);
  }
  else if(digitalRead(11) == LOW)
  {
    msg = "4";
    digitalWrite(13, true);
  }
  else
    msg = "0";
   
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  digitalWrite(13, false);

}


Rx:
Code:
#include <VirtualWire.h>
char lastchar = '0';

void setup()
{
  Serial.begin(9600);    //Debugging
  Serial.println("setup");

  //Inisial IO dan ISR
  vw_set_ptt_inverted(true); // ibutuhkan untuk DR3100
  vw_setup(2000);     //Bits per sec
  vw_set_rx_pin(2);
  vw_rx_start();       //memulai receiver

  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen))
  {
    // menerima pesan checksum
    Serial.print("Menerima: ");
    Serial.print(buf[0]);
    Serial.print(" ");

    if(buf[0] == '1' && buf[0] != lastchar )
    {
      digitalWrite(8, !digitalRead(8));
    }
    else if(buf[0] == '2' && buf[0] != lastchar )
    {
      digitalWrite(9, !digitalRead(9));
    }
    else if(buf[0] == '3' && buf[0] != lastchar )
    {
      digitalWrite(10, !digitalRead(10));
    }
    else if(buf[0] == '4' && buf[0] != lastchar )
    {
      digitalWrite(11, !digitalRead(11));
    }
    lastchar = buf[0];
  }
}
806  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 02:54:07 pm
What LED is flashing, the onboard LED next to pin 13?
807  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 12:48:17 pm
(5V)-------------{button}---x-------<resistor>------(Gnd)
                                                          |
                                                          |
                                                  (Ard pin)
808  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 04:15:45 am
Did you remember the pulls own resistors for each of the buttons?
809  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 03:29:14 am
So wait you want to toggle it? Press once = ON, press again = OFF?
810  Using Arduino / Programming Questions / Re: Question 433mhz transmitter and receiver. on: March 24, 2014, 12:40:16 am
Your receiver code was turning the LEDs off because they were being told to turn off at the beginning of every loop. However, if you only turn off the LED when you don't receive anything then they should not flicker.

RX
Code:
#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);    //Debugging
    Serial.println("setup");

    //Inisial IO dan ISR
    vw_set_ptt_inverted(true); // ibutuhkan untuk DR3100
    vw_setup(2000);     //Bits per sec
    vw_set_rx_pin(2);
    vw_rx_start();       //memulai receiver

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen))
    {
    int i;

/*lampu menyala sebagai indikator pesan terkirim*/
        digitalWrite(13, true);
       // menerima pesan checksum
       Serial.print("Menerima: ");
   
    for (i = 0; i < buflen; i++)
    {
        Serial.print(buf[i]);
        if(buf[i] == '1'){digitalWrite(8, HIGH);}
        if(buf[i] == '2'){digitalWrite(9, HIGH);}
        if(buf[i] == '3'){digitalWrite(10, HIGH);}
        if(buf[i] == '4'){digitalWrite(11, HIGH);}
        Serial.print(" ");
    }
    Serial.println("");
        digitalWrite(13, false);

   }
   else // if you get nothing, then LED off.
   {
     digitalWrite(8, LOW);
     digitalWrite(9, LOW);
     digitalWrite(10, LOW);
     digitalWrite(11, LOW);
   }
}
Pages: 1 ... 52 53 [54] 55 56 ... 242