Go Down

Topic: Turn on LED > push button > take and transfer picture (Read 2 times) previous topic - next topic

murple

#15
Nov 26, 2011, 10:32 pm Last Edit: Nov 27, 2011, 12:51 am by murple Reason: 1
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: [Select]
/*
 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
}

PaulS

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

Code: [Select]
   if ( Serial.read() == 1) {
which then expects the integer value 1, which is not the same as the character '1'.
Code: [Select]

byte b = Serial.read() - '0';
if(b == 1)
{
  // The character '1' arrived...
}

or
Code: [Select]
   if ( Serial.read() == '1') {

murple

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') {".

AWOL

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.
"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.

murple

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: [Select]
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: [Select]
##################################################
##################################################
#                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

Go Up