Arduino mega program (need help)

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
king_king
 
Posts: 18
Joined: Fri Mar 30, 2012 4:53 am

Arduino mega program (need help)

Post by king_king »

i need help to make program to count change in switch state by using matlab code

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino mega program (need help)

Post by Franklin97355 »

We can help if you post your code and tell us what the problem is. Also how you have the parts connected to each other.

king_king
 
Posts: 18
Joined: Fri Mar 30, 2012 4:53 am

Re: Arduino mega program (need help)

Post by king_king »

i don't have the matlab code that count increment


/ this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}


void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;


// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

}

i need the same code for matlab

hjohnson
 
Posts: 78
Joined: Sun Dec 20, 2009 9:17 pm

Re: Arduino mega program (need help)

Post by hjohnson »

Firstly, why? It's usually a good idea to push things like button presses as far down the food chain as possible, then have higher level functions just ask them for the count when they need it. The Arduino should handle the counting, because it's good at it and handles it very quickly. Matlab should then simply send a command asking for the current count, and the Arduino should reply with a number.

Another few notes for your Arduino code:
1. You should check out using a pin change interrupt, it could simplify your code immensely. Check out: http://www.arduino.cc/en/Reference/AttachInterrupt for more details.

2. Have you tested that code to check that the switch isn't horribly messy and noisy? Unless you've implemented some debouncing externally, I'd be shocked if it only incremented once per button press.

Now, to MATLAB: If you *really* want to go down this road (and I'd seriously not recommend it), one (horribly inefficient) way that you could implement it would be to have the Arduino constantly print the current state of that pin to the Serial line, then read that data using Matlab (check out http://www.mathworks.com/help/techdoc/m ... 38496.html) and use pretty much the same logic that you use in your arduino code- read the value, and compare it with the last value. If they aren't equal, then that's a change.

Harry

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”