Go Down

Topic: new to this, using copied code, getting errors (Read 527 times) previous topic - next topic

coasterp

I am trying to get a project working using code taken from on line. The code is:

#include <Arduino.h>
#include <Keyboard.h>
const int LEDPIN = 13;  //LED connected to pin 13:
const int SENSORPIN = 3;  //sensorpin connected to pin 3:
// variables will change:
int sensorState = 0, lastState = 0; // variable for reading the pushbuton status:
void setup () {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  pinMode(SENSORPIN, INPUT);
  digitalWrite(SENSORPIN, LOW); // turn on the pullup:
  Serial.begin (9600);
}
void loop () {
  //read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);
  //check if the sensor beam is broken
  //if it is, the sensorState is HIGH:
  if (sensorState == HIGH) {
    //turn on led:
    digitalWrite(LEDPIN, LOW);
  }
  else {
    //turn off led:
    digitalWrite(LEDPIN, HIGH);
  }
  if (sensorState && !lastState) {
    Serial.println("unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
    //begin keyboard communication:
    Keyboard.begin();
    delay(2000);
    //send a keystroke:
    Keyboard.write('s');
    //end keyboard communication:
    Keyboard.end();
  }
  lastState = sensorState;

I am getting the following:

exit status 1
stray '\357' in program

I know nothing about coding. Just trying to reproduce something that another individual has done. They know very little about coding and are not able to figure this out. Any assistance is greatly appreciated.

pert

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]// your code is here[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

For some reason code copied from online occasionally contains hidden or non-standard characters that cause that error. When you post the code on the forum it strips out these characters. So if you copy and paste this code it should work for you:
Code: [Select]
#include <Arduino.h>
#include <Keyboard.h>
const int LEDPIN = 13;  //LED connected to pin 13:
const int SENSORPIN = 3;  //sensorpin connected to pin 3:
// variables will change:
int sensorState = 0, lastState = 0; // variable for reading the pushbuton status:
void setup () {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  pinMode(SENSORPIN, INPUT);
  digitalWrite(SENSORPIN, LOW); // turn on the pullup:
  Serial.begin (9600);
}
void loop () {
  //read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);
  //check if the sensor beam is broken
  //if it is, the sensorState is HIGH:
  if (sensorState == HIGH) {
    //turn on led:
    digitalWrite(LEDPIN, LOW);
  }
  else {
    //turn off led:
    digitalWrite(LEDPIN, HIGH);
  }
  if (sensorState && !lastState) {
    Serial.println("unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
    //begin keyboard communication:
    Keyboard.begin();
    delay(2000);
    //send a keystroke:
    Keyboard.write('s');
    //end keyboard communication:
    Keyboard.end();
  }
  lastState = sensorState;
}

coasterp

It appears like what you posted may work. Thanks you so much for your assistance.

coasterp

Need some more assistance. In the above code, what is supposed to happen is when a photo sensor beam is broken, after a delay of 2 seconds, the Arduino is to tell the computer it's connected to to type the letter S. I am trying to use this code to accomplish that but am not having success. The difference is I do not have the photo sensor hooked up. I am instead trying to trigger the S to be typed by sending 5 volts to pin 3. So far I have not been able to get this to work. I am unsure if pin 3 needs to be configured differently. I have been reading up on pin assignments but have not had any luck getting the info I need. Once again, any assistance is greatly appreciated. My plan is to take the 5VDC off the board, run it through a relay and return it to pin 3 on the board. The relay is being controlled by a PLC and will close for 200 ms. When that happens, I need the Arduino to tell the computer to type the letter S.

Delta_G

You say you're connecting 5V with a wire to pin 3.  When the wire with 5V isn't connected, what is connected to pin 3?  If you said nothing, then google "floating input" and learn about pull-up and pull-down resistors. 

Here's a good link on the topic.

http://www.gammon.com.au/switches
If at first you don't succeed, up - home - sudo - enter.

coasterp

Thanks. That actually explained a lot. I'm familiar with working with PLC's but not these boards. May just go to the photosensors and keep the whole scoring system separate from my pinsetter control system. In case you wanted to see it, here is a link to what I've built: https://www.youtube.com/watch?v=qdBQLyg2Zq8&t=13s

saildude

@Delta_G

Thanks for the link with things in one nice package - I hope it is OK to ask about one of the circuits -

The Pull Down Resistor - is there and advantage / disadvantage or just don't do it to adding a small cap across the 10k resistor?

Because of my industrial background I was always taught to use High to turn something on or hold something locked in - theory being if a wire broke things would stop or not turn on.  Some years back someone had a go low to start something and a wire shorted to some conduit and a bridge section was activated and one person was killed - a bit obscure circuit but I am always cautious since much of the equipment I used to work on could seriously hurt someone if something went wrong.

I also have trouble with the thinking process of using low to start something in the code.

Thanks

Delta_G

You put the pull-up right next to the pin, so if the wire breaks it goes high.  It works both ways. 
If at first you don't succeed, up - home - sudo - enter.

saildude


OldSteve

#9
Nov 11, 2017, 07:22 am Last Edit: Nov 11, 2017, 07:23 am by OldSteve
Thanks
Just to add to what Delta_G said, (incidentally, she's well-versed and knows what she's talking about), after reading your posts - regardless of whether you're looking at a high or a low as being the active state, with CMOS input technology you can never be sure if an input will be high or low if left floating. It 'hovers' about the midpoint, and pretty well decides by luck on which way it will go. Unlike the earlier TTL circuitry, which will stay low unless a voltage or pullup resistor is applied to turn on the input transistor.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Delta_G

Quote
Delta_G said, (incidentally, she's
Not a she.
If at first you don't succeed, up - home - sudo - enter.

OldSteve

Not a she.
Oh no, what a blunder. I'm very sorry for that. I just assumed that your forum name was taken from Delta Goodrem's, and that you must have also been female.
(You know what they say about assuming. :D )
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Delta_G

Oh no, what a blunder. I'm very sorry for that. I just assumed that your forum name was taken from Delta Goodrem's, and that you must have also been female.
(You know what they say about assuming. :D )
No, my name comes from the symbol for the change in Gibbs free energy.  I'm a chemist by trade.
If at first you don't succeed, up - home - sudo - enter.

AWOL

Quote
Gibbs free energy
Free energy from toothpaste?
"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.
I speak for myself, not Arduino.

Delta_G

Free energy from toothpaste?
Enthalpy minus the product of the temperature and the entropy
If at first you don't succeed, up - home - sudo - enter.

Go Up