Pages: 1 [2]   Go Down
Author Topic: Turn on LED > push button > take and transfer picture  (Read 1959 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL: Thanks for the feedback. I was under the impression that the scope of variables and objects created in the void setup meant they were accessible throughout the rest of the code, but by the sounds of it that's quite wrong. I've removed the code relating to the variable and object from the void setup and moved them both into PushButton() like so. Will this work as intended now?

Code:
/*
  1. Computer sends a "1" over the serial connection (USB)
  2. Arduino turns on LED
  3. Arduino servo pushes button, waits, returns to starting position
  4. Arduino sends a "1" back to computer to let it now the cycle is complete
  5. Computer takes a snapshot with the USB webcam/microscope
  6. Computer OCRs image and returns 6 digits to user (e.g copied to clipboard or via webpage)
 */


  //Includes
  #include <Servo.h> ;


  //Constants
  const int PinServo = 9;
  const int PinLed1 = 11;
  const int PinLed2 = 12;
  const int PinLed3 = 13;


  //Setup
void setup() {                
  Serial.begin(9600);
  pinMode(PinLed1, OUTPUT);
  pinMode(PinLed2, OUTPUT);
  pinMode(PinLed3, OUTPUT);
}


void loop() {
  if (Serial.available()){  
   if ( Serial.read() == 1) {
     TurnOnLights();
     PushButton();
     InformPcComplete();
     delay(4000); //Allow PC sufficient time to take picture. Value needs tweaking.
     TurnOffLights();
   }
  }
}


void TurnOnLights() { //TurnOnLights and TurnOffLights should be combined into ControlLights(Argument)
  digitalWrite(PinLed1, HIGH);
  digitalWrite(PinLed2, HIGH);
  digitalWrite(PinLed3, HIGH);
}


void TurnOffLights() {
  digitalWrite(PinLed1, LOW);
  digitalWrite(PinLed2, LOW);
  digitalWrite(PinLed3, LOW);
}


void PushButton() {
  Servo ServoButton;
  ServoButton.attach(PinServo);
  ServoButton.write(90);      // Servo moves to pos 90
  delay(1000);
  ServoButton.write(0);       // Servo returns to pos 0
  delay(1000);
}


void InformPcComplete() {
  Serial.print(1);            // Tell PC cycle is complete
}
« Last Edit: November 26, 2011, 06:51:17 pm by murple » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
ser.write('1')
will send the character 1 to the Arduino.

Code:
   if ( Serial.read() == 1) {
which then expects the integer value 1, which is not the same as the character '1'.
Code:
byte b = Serial.read() - '0';
if(b == 1)
{
  // The character '1' arrived...
}
or
Code:
   if ( Serial.read() == '1') {
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS: That makes perfect sense, well done! That's exactly the sort of thing that might have been very difficult for me to figure out on my own. I think I'll try your second recommendation "if ( Serial.read() == '1') {".
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was under the impression that the scope of variables and objects created in the void setup meant they were accessible throughout the rest of the code
No, quite the wrong impression.
"loop" and "setup" are functions just like any other; no special scope rules apply to them.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL: Understood, thanks.

PaulS: I've tried to correct the code of the Arduino and Python to only use integers. I think I've got it for now. Still waiting to receive my first Arduino...

Code:
void loop() {
  if (Serial.available()){ 
   if ( Serial.read() == 1) {
     TurnOnLights();
     PushButton();
     InformPcComplete();
     delay(4000); //Allow PC sufficient time to take picture. Value needs tweaking.
     TurnOffLights();
   }
  }
}

void InformPcComplete() {
  Serial.print(1);            // Tell PC cycle is complete
}

python code:
Code:
##################################################
##################################################
#                Communicate with Arduino
##################################################
##################################################
print "Sending a 1 to Arduino..."
#Send a 1 (integer!) over serial to the Arduino, telling it to turn on the lights and push the button
ser = serial.Serial('/dev/tty.usbserial', 9600)
ser.write(1)

print "Waiting to receive a 'finished' from the Arduino..."
#Wait until receive the OK from the Arduino, meaning it has finished all it's tasks.
while 1
 msgarduino = ser.readline()
 if msgarduino == 1
  break
Logged

Pages: 1 [2]   Go Up
Jump to: