4

I am not able to post my code properly. If I post my complete code, both the preview and the final post look horrible.

Here I am posting my code using {} symbol.


import threading import queue import serial import time import RPi.GPIO as GPIO

---------------------------------------LCD-----------------------------------------------------

LCD_RS = 23 LCD_E = 24 LCD_D4 = 12 LCD_D5 = 16 LCD_D6 = 20 LCD_D7 = 21

LED_ON = 15

Define some device constants

LCD_WIDTH = 20 # Maximum characters per line LCD_CHR = True LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

Timing constants

E_PULSE = 0.0005 E_DELAY = 0.0005

---------------------------------------------------------------------------------------------

count_enable=0 count=0

i=0

flag=0 transmit_flag=0

rbuff=[]

rbuff=list(range(100)) temp=0 temp1=0

start=0

zero_detect=1

print("staring program") ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT)

events_queue = queue.Queue()

start1 = time.time()

start2 = time.time()

-------------------------------------------LCD----------------------------------------

def lcd(): # Main program block

# GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setwarnings(False) GPIO.setup(LCD_E, GPIO.OUT) # E GPIO.setup(LCD_RS, GPIO.OUT) GPIO.setup(LCD_D4, GPIO.OUT) # DB4 GPIO.setup(LCD_D5, GPIO.OUT) # DB5 GPIO.setup(LCD_D6, GPIO.OUT) # DB6 GPIO.setup(LCD_D7, GPIO.OUT) # D7

# Initialise display lcd_init()

def lcd_init(): # Initialise display

lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

def lcd_byte(bits, mode): # Send byte to data pins # bits = data # mode = True for character # False for command

GPIO.output(LCD_RS, mode) # RS

# High bits GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x10==0x10: GPIO.output(LCD_D4, True) if bits&0x20==0x20: GPIO.output(LCD_D5, True) if bits&0x40==0x40: GPIO.output(LCD_D6, True) if bits&0x80==0x80: GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin lcd_toggle_enable()

# Low bits GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x01==0x01: GPIO.output(LCD_D4, True) if bits&0x02==0x02: GPIO.output(LCD_D5, True) if bits&0x04==0x04: GPIO.output(LCD_D6, True) if bits&0x08==0x08: GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin lcd_toggle_enable()

def lcd_toggle_enable(): # Toggle enable time.sleep(E_DELAY) GPIO.output(LCD_E, True) time.sleep(E_PULSE) GPIO.output(LCD_E, False) time.sleep(E_DELAY)

def lcd_string(message,line,style): # Send string to display # style=1 Left justified # style=2 Centred # style=3 Right justified

if style==1:
    message = message.ljust(LCD_WIDTH," ")
if style==2:
    message = message.center(LCD_WIDTH," ")
elif style==3:
    message = message.rjust(LCD_WIDTH," ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_backlight(flag): # Toggle backlight on-off-on GPIO.output(LED_ON, flag)

-----------------------------------------------------------------------------------------

def thread_read(): global count_enable, flag,temp, count,start2,start start=0 lcd() while True: rbuff=ser.read() if count_enable == 1: count+=1 if rbuff[0] == 0x02 and flag==0: # and zero_detect==1: count_enable=1 #ser.write(rbuff) #print (count)b flag=1 if rbuff[0] == 0x01 and flag==1 and count==1: #ser.write(rbuff) #print(count) flag=2 if rbuff[0] == 0x04 and flag==2 and count==2: #ser.write(rbuff) #print(count) flag=3 if rbuff[0] == 0x03 and flag==3 and count==3: #ser.write(rbuff) #print(count) flag=4

    if flag==4 and count==28:
        ser.write(rbuff)
        lcd_string(str(rbuff),LCD_LINE_2,2)
        count=0
        flag=0

        if (rbuff[0]-2 <= temp) and  (temp<= rbuff[0]+2):
            GPIO.output(18,GPIO.HIGH)
            ser.write(rbuff)
            #start=0
            temp=rbuff[0]
            print(GPIO.input(18))


        else:
            GPIO.output(18,GPIO.LOW)
            #start=0
            start=10+(time.time())
            #print(start)
            temp=rbuff[0]
            print(start)
            # ser.write(ord(buff))
            #print("ON")

        if start==0:
            GPIO.output(18,GPIO.LOW)

        else:
            GPIO.output(18,GPIO.HIGH)
            now=time.time()
            seconds=start-now
            #print(seconds)
            if seconds>0:
                GPIO.output(18,GPIO.HIGH)
                #print("on")
            else:
                GPIO.output(18,GPIO.LOW)
                #print("off1")
                start=0

    if count == 1 and rbuff[0] != 0x01:
        count_enable=0
        count=0
        flag=0

def thread_check():

global start1

while True:

if time.time() - start1 >= 1:

start1 = time.time()

        #print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read) read_thread.start()

check_thread = threading.Thread(target=thread_check)

check_thread.start()

event=threading.Event() try:
event.wait() except KeyboardInterrupt: pass

  • 1
    select all of the code and click {} .... or, insert three backticks ``` before the code and three after the code .... or, indent the code by at least three spaces .... copy the code into a text editor, such as notepad++, and insert three spaces before each line .. then copy and paste into your question – jsotola Aug 7 '19 at 5:38
  • 1
    upvote for asking ... so many people paste garbage looking code and do not even give it any thought ... of course, those same people want to be spoon fed finished code – jsotola Aug 7 '19 at 5:42
  • Yes, the trick is first paste code, then click { }. I remember if I clicked first, then pasted code later, I got your same problem. I think this is a system bug (not sure if stkex system wide, or only rpt.stkex), or poor, misleading, inconsistent instructions. – tlfong01 Aug 7 '19 at 6:04
  • Hi jsotola, It works. Thanks. – Arpit Aug 7 '19 at 8:26
4

Here i am posting my code using {} symbol.

Definitely not; you've misunderstood something or have some defunct keys on your board ;) Looking at the raw text of your question, the code appears to have been cut n' pasted in unmodified.

If you look in the top right corner of the interface, you'll find this:

enter image description here

Excuse my inability to draw a straight line with a touchpad. One of the links in that menu is to here, where you will hopefully find answers to most questions about using the site.

But unfortunately, not formatting. However, if you look at the top bar of the actual editor box, you'll see that mysterious symbol appears again on the right side.

enter image description here

This time if you click it another menu bar, of text, will drop down. One of the choices there is Code and if you click that, you will get a lots of instructions.

The short version:

  • If you copy code to your clipboard, click the {} icon in the top bar. You'll see 'enter code here' as a selection by the cursor, click that and paste. What that should do is add an extra four spaces as indent, which formats the code. If not, something went wrong. Mouse selections can be very slippery beasts.

    Note the {} isn't literal, it's a symbol that's supposed to remind you of code. Just not python ;)

  • You can also select a region of text, then click that symbol, and it should be so formatted.

  • To format code inline, use single backticks.

The editor uses markdown for formatting. You will find lots and lots of documentation for it online (e.g., the original spec from almost 15 years ago). It is also described in the documentation here. Note that the implementation is not complete but includes the basic stuff.

Markdown has become very popular in the programming world, particularly web-dev, probably because people who write code all day will probably find it easier to include an asterisk or backtick while typing instead of going for the mouse and dragging a pointer around. It is often used by documentation systems. And online editors.

  • Your first bullet point is wrong. – wizzwizz4 Aug 19 '19 at 15:26
  • @wizzwizz4 Perhaps you could explain what you think is wrong? As far as I can tell, goldilocks is completely correct here, but it's difficult to tell what you don't agree with from that comment—"it's wrong" isn't particularly helpful. – Aurora0001 Aug 20 '19 at 9:30
  • You can't paste after clicking the {} marks – or, rather, you can, but it won't put in code formatting. – wizzwizz4 Aug 20 '19 at 9:35
  • @wizzwizz4 It works fine for me in Chrome and even IE11... not sure if you're using a different browser where that doesn't work, but it seems okay to me. – Aurora0001 Aug 20 '19 at 9:43
  • @Aurora0001 I'm using Firefox, but I've read on meta before that this feature doesn't exist. – wizzwizz4 Aug 20 '19 at 9:58
  • @wizzwizz4 Works fine for me too. How about highlighting the block instead, then clicking {}? – goldilocks Aug 20 '19 at 14:12
1

The simple solution to formatting code is to use the back-tick (`CODE HERE`), found next to the 1 button on an English QWERTY keyboard.

For multi-line code, use three or more back-ticks like this:

```  
CODE HERE  
```

You can add highlighting by specifying a language by adding the name next to the opening back-ticks, for example:

```python  
CODE HERE  
```

You can also use three tilde characters (~~~) instead of back-ticks (```). See Meta Stack Exchange for more information.

  • I just edited to clarify this as you only need three backticks to get the code formatting, and added a reference link from Meta, hope this is helpful. This is a useful suggestion though — thanks for posting it. – Aurora0001 Aug 17 '19 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .